Passing data from middleware to api route without exposing on response
Answered
Northeast Congo Lion posted this in #help-forum
Northeast Congo LionOP
Everything I read says to either set cookies or headers to share data from the middleware to an API route. How do I do this without also echoing that data to the end user in the final response?
If I try to
I also noticed that I cannot add an
I use
Is there any way to let my route handler entirely remove the header (or cookie) set by the middleware? Is there some other way to pass data that's deployment environment agnostic? If I can't then I just end up duplicating a bit of boilerplate in each handler, which isn't the end of the world...
I'm on next 13.4.13.
If I try to
response.header.delete() my header, it lets the original show up in the response. If I set my header to an empty string in my route handler, then the header is empty in the response (similarly for undefined or null). This is undesirable because I don't want to expose the name of my header.I also noticed that I cannot add an
Authentication header to the request from the middleware -- it silently gets left as undefined when my API route tries to retrieve it.I use
NextResponse.json to create my response. If I use plain new NextResponse and explicitly set just the content-type header, the extra header set by middleware is also appended.Is there any way to let my route handler entirely remove the header (or cookie) set by the middleware? Is there some other way to pass data that's deployment environment agnostic? If I can't then I just end up duplicating a bit of boilerplate in each handler, which isn't the end of the world...
I'm on next 13.4.13.
Answered by tafutada777
i am not sure exactly what you want to do but the official sample code won't expose headers added in middleware. in the sample,
https://nextjs.org/docs/app/building-your-application/routing/middleware#setting-headers
https://nextjs.org/docs/app/building-your-application/routing/middleware#setting-headers
5 Replies
i am not sure exactly what you want to do but the official sample code won't expose headers added in middleware. in the sample,
https://nextjs.org/docs/app/building-your-application/routing/middleware#setting-headers
https://nextjs.org/docs/app/building-your-application/routing/middleware#setting-headers
Answer
curl -v http://...
< HTTP/1.1 200 OK
< date: Wed, 16 Aug 2023 01:56:51 GMT
< x-hello-from-middleware2: hello
< vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding
< content-type: application/json
< connection: close
< transfer-encoding: chunked
<
* Closing connection 0You shouldn't set the data to response, use
Response will be sent to the client, but only your route handlers can receive the request
request.header insteadResponse will be sent to the client, but only your route handlers can receive the request
@tafutada777 curl -v http://...
< HTTP/1.1 200 OK
< date: Wed, 16 Aug 2023 01:56:51 GMT
< x-hello-from-middleware2: hello
< vary: RSC, Next-Router-State-Tree, Next-Router-Prefetch, Accept-Encoding
< content-type: application/json
< connection: close
< transfer-encoding: chunked
<
* Closing connection 0
Northeast Congo LionOP
I see now. I was just calling
request.headers.set('X-Syndication-Token', 'foo'); but I see I have to explictily tell the NextResponse.next() call to use the new headers. Thanks for the tip.Chalcid wasp
its high time we need this https://github.com/vercel/next.js/discussions/54998