Next Response Headers
Answered
Japanese flying squid posted this in #help-forum
Japanese flying squidOP
I want to set every api response content type to application/json (i am using it in Next.config.mjs file) , but it's getting overrided by default text/plain content type. I wanna achieve behavior so every api response has content type application/json and in case I want to change it, I will change when sending response like
return new Response(JSON.stringify(data), { status: 200, headers: { "Content-Type": "text/plain" }});
return new Response(JSON.stringify(data), { status: 200, headers: { "Content-Type": "text/plain" }});
44 Replies
Japanese flying squidOP
up
Japanese flying squidOP
up
up
Japanese flying squidOP
up
can i ask why you dont just specify
text/json in new Response and not lock yourself into always json + Response.json() could simplify your life@riský can i ask why you dont just specify `text/json` in `new Response` and not lock yourself into always json + `Response.json()` could simplify your life
Japanese flying squidOP
What you mean, can you explain better?
1. whats wrong with just adding
text/json for better clarity on everything2.
Response.json() does the json.stringify and adds headersAnswer
Japanese flying squidOP
Ooooh thank you so much
one other question please
I am getting this error when accessing my API Routes from other just index.htmk / vanila Javascript page,
how can I make everything allow access my API routes?
how can I make everything allow access my API routes?
@riský
ur api is external?
why is it on a different port
@Japanese flying squid I am getting this error when accessing my API Routes from other just index.htmk / vanila Javascript page,
how can I make everything allow access my API routes?
hmm can you open a new thread as this is kinda diferent (can get more help + i am about to sleep anyway)
ill help him
but can you still make a new one as cors is madness and should be its own topic
set cors headers for response
Japanese flying squidOP
@gin I just created Training API for my kid students in school and I want to use it as just simple REST Api backend. But they can't access it
@gin its express?
Japanese flying squidOP
No, next.js APP
u created a next app just for an api? or because of your landingpage aswell
Japanese flying squidOP
For both of them, yes.
so u use app router for your api
Japanese flying squidOP
yes
Japanese flying squidOP
@gin yes it helps but how to automatically do it for every API route? instead of setting every header one by one
@gin https://nextjs.org/docs/app/api-reference/next-config-js/headers#cors
Japanese flying squidOP
Yes, I saw this but can't understand, where?
@Japanese flying squid Yes, I saw this but can't understand, where?
in ur config file
Japanese flying squidOP
omg how dumb I am
next.config.js
Japanese flying squidOP
I was setting it in middlware config
instead of config.js
omg
thank you so much
Japanese flying squidOP
@gin
still have a problem
