1

i code in nodejs. i have for example an object

var foo = {"a": "b"}

But when i do

res.send(JSON.stringify(foo))

postman respons me by default as an HTML. res.send(foo) , the same thing... But I want to make postman respond me a valid JSON by default how to do it ?

J. Doe
  • 39
  • 2
  • I guess you are using express. You need to configure it to return json format, as explained [here](https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express) – Orelsanpls Aug 25 '17 at 09:30

1 Answers1

1

In order for your code to return a proper json object you need to specify in the head what are you returning;

response.writeHead(200, {"Content-Type": "application/json"});
var foo = {"a": "b"}
var json = JSON.stringify(foo);
response.end(json);

or if you use express just use:

res.json(json);
Alexandru Olaru
  • 6,842
  • 6
  • 27
  • 53