Sometimes, we want to pass JSON to HTTP POST request made with Node.js request.
In this article, we’ll look at how to pass JSON to HTTP POST request made with Node.js request.
How to pass JSON to HTTP POST request made with Node.js request?
To pass JSON to HTTP POST request made with Node.js request, we call request
with the json
option set to true
and the body
option set to a plain JavaScript object with the request body.
For instance, we write
const rp = require('request-promise');
await rp({
method: 'POST',
uri: 'http://localhost:3000/',
body: {
val1: 1,
val2: 2
},
json: true
})
to call rp
with the body
set a request body object.
And we set json
to true
to send the body
as a JSON payload.
We set method
to 'POST'
to make a POST request.
Conclusion
To pass JSON to HTTP POST request made with Node.js request, we call request
with the json
option set to true
and the body
option set to a plain JavaScript object with the request body.