Categories
JavaScript Answers

How to POST data with request module on Node.js?

Spread the love

To POST data with request module on Node.js, we call the request.post method.

For instance, we write

const request = require("request");

request.post(
  {
    headers: { "content-type": "application/x-www-form-urlencoded" },
    url: "http://localhost/test2.php",
    body: "mes=heydude",
  },
  (error, response, body) => {
    console.log(body);
  }
);

to call request.post with an object that has the headers, url and the request body.

We get the response and the response body from the callback.

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 *