Categories
JavaScript Answers

How to send headers with form data using request module with Node.js?

Spread the love

To send headers with form data using request module with Node.js, we call the post method.

For instance, we write

const req = require("request");

req.post(
  {
    url: "someUrl",
    form: {
      username: "user",
      password: "",
      opaque: "someValue",
      logintype: "1",
    },
    headers: {
      "User-Agent":
        "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36",
      "Content-Type": "application/x-www-form-urlencoded",
    },
    method: "POST",
  },

  (e, r, body) => {
    console.log(body);
  }
);

to call post with an object with the form property set to an object with the form data key-value pairs to send them as the post request body.

We get the response from the body parameter in 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 *