Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nodejs & express 4, res.json returns "Content-Type: text/plain"

I'm trying to make my application return "application/json" as the content type whenever the response is a json of course.

I have tried:

res.json(jsonContent);

response header has "Content-Type → text/plain; charset=utf-8"

and

res.setHeader('content-type', 'text/json');
res.send(jsonContent); 

response header has "Content-Type → text/plain; charset=utf-8"

and

res.set('content-type', 'text/json');
res.send(jsonContent); 

response header has "Content-Type → text/plain; charset=utf-8"

and all of the above at the same time. But my app always server responses as text/plain and never application/json. What might I be doing wrong?

like image 454
just_user Avatar asked Jan 08 '23 07:01

just_user


2 Answers

As the documentation explains:

res.type('json');               // => 'application/json'
res.type('application/json');   // => 'application/json'

Sets the Content-Type HTTP header to the specified type.

like image 122
Ash Avatar answered Jan 21 '23 20:01

Ash


Depending on your setup (and applied middleware), if the client's request was not made with Accept: application/json, then the response's content type might be getting set to just text/plain.

like image 29
Jaymes Bearden Avatar answered Jan 21 '23 20:01

Jaymes Bearden