To send Content-Type: application/json post with Node.js, we call the request
function.
For instance, we write
const request = require("request");
const options = {
uri: "https://www.example.com/urlshortener/v1/url",
method: "POST",
json: {
longUrl: "http://www.google.com/",
},
};
request(options, (error, response, body) => {
if (!error && response.statusCode === 200) {
console.log(body.id);
}
});
to call request
with the options
object to make a request.
The method
is set to 'POST'
to make a post request.
The json
property is set to an object with the JSON request body data.