Sometimes, we want to get a JSON via HTTP request in Node.js.
In this article, we’ll look at how to get a JSON via HTTP request in Node.js.
How to get a JSON via HTTP request in Node.js?
To get a JSON via HTTP request in Node.js, we can call request
with json
set to true
.
For instance, we write
const options = {
hostname: "127.0.0.1",
port: app.get("port"),
path: "/users",
method: "GET",
json: true,
};
request(options, (error, response, body) => {
console.log(body);
});
to call request
with json
set to true
in the options
object to return a JSON response.
Then we get the parsed JSON response body from the body
parameter in the callback.
Conclusion
To get a JSON via HTTP request in Node.js, we can call request
with json
set to true
.