Comments (4)
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:
next.js/packages/next/src/server/api-utils/node/api-resolver.ts
Lines 120 to 125 in ac46ffe
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.
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.
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.
Thanks @icyJoseph, that work around does the job! Cheers! 😃
from next.js.
Related Issues (20)
- Turbopack: Failed to compile (turbo) with (react-pdf) HOT 1
- `Request` object behaves differently in fetch HOT 7
- navigating back from multiple intercepted route breaks routing
- Turbopack: Unable to load SVG. Uncaught Error: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object.
- Middleware added Nonce attributes missing from TypeScript production builds
- typedRoutes don't recognize parameter on rewritten url if preceded by path character
- Github actions: Error: Page "/logs/publish/[id]" is missing "generateStaticParams()" so it cannot be used with "output: export" config. HOT 1
- Next 14 shows flicker with external React Native components on web
- Preflight cache middleware opt-out not working for rewrite of dynamic routes
- [Turbopack] [Next 15 RC] Cannot resolve exported paths from `@skeletonlabs/skeleton@next` HOT 2
- redirect in NextJS config doesn't populate param in `destination` more than once
- The Edge Function "middleware" size is 1.01 MB and your plan size limit is 1 MB. Though the file is 173kb HOT 2
- Subresource Integrity (SRI) not working HOT 1
- global-error.tsx doesn't work next.js 14+
- [Next 15 RC] create-next-app --empty with tw and src dir has wrong content path HOT 3
- CORS middleware not applied to preview and staging domains, but works on production domain
- From my testing, when GET and PUT are the only methods set, it breaks. I also tried changing the order in which they are set.
- Request method bug in production environment. HOT 3
- Next.js doesn't resolve docker network defined hostnames HOT 1
- `npm start`/`next start` fails HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from next.js.