Skip to content

fetch remote image with cors #242

Description

@janwagner

Hi.

I am trying to use next-connect to load images from a remote host.
I am relatively new to next.js api routes and would be very grateful for any help.

Has anyone ever tried something similar, is it even possible to retrieve an image this way and thus to handle cross-origin requests?

My goal is to either have the image as a blob in the end, or directly convert it directly to base64

pages/api/image.ts
import { createRouter } from 'next-connect'
import cors from 'cors'
import type { NextApiRequest, NextApiResponse } from 'next'

const image = 'http://placekitten.com/g/1000/1000'

const router = createRouter<NextApiRequest, NextApiResponse>()
  .use(cors())
  .get(async (req, res) => {
    const response = await fetch(image)
    const data = response.blob

    res.json(data)
  })

export default router.handler()

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions