Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fetch remote image with cors #242

Open
janwagner opened this issue Jan 30, 2024 · 0 comments
Open

fetch remote image with cors #242

janwagner opened this issue Jan 30, 2024 · 0 comments

Comments

@janwagner
Copy link

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant