Git Product home page Git Product logo

Comments (4)

icyJoseph avatar icyJoseph commented on June 18, 2024

Is the issue here that, .json implicitly, sets the header for you though?

That one assigns the sendJson function to the NextApiReponse object, which in turn:

Forces the header to application/json, somewhat expectedly. I don't see why it wouldn't do that.

To send a pdf, you need to do something different. You can search for a solution, most use a stream to send the chunks to the client.

from next.js.

AshConnolly avatar AshConnolly commented on June 18, 2024

Thanks for the reply @icyJoseph! 😃

I'm actually not trying to send a pdf. I need to send a custom content-type header, like 'application/hello+json', that will send a json payload. This is because the content type needs to be specific for the BE I'm working with (that I don't control).

Do you know how I would achieve this? How do I get around next js overwriting my setHeader()?

I've searched high and low and found little information on how to do this for the pages router. The docs have very little about it.

from next.js.

icyJoseph avatar icyJoseph commented on June 18, 2024

Use the send helper:

// Next.js API route support: https://nextjs.org/docs/api-routes/introduction
import type { NextApiRequest, NextApiResponse } from 'next';

export default function handler(req: NextApiRequest, res: NextApiResponse) {
  res.setHeader('Content-Type', 'application/pdf'); 
  res.status(200).send(JSON.stringify({ name: 'John Doe' }));
}

Then again, this will really cause some strange behavior, because a browser might try to actually show a pdf if you go to that endpoint.

from next.js.

AshConnolly avatar AshConnolly commented on June 18, 2024

Thanks @icyJoseph, that work around does the job! Cheers! 😃

from next.js.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.