Categories
JavaScript Answers

How to send Content-Type: application/json post with Node.js?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *