Categories
JavaScript Answers

How to create a Node HTTP Client Request with a cookie?

Spread the love

To create a Node HTTP Client Request with a cookie, we call the http.request method.

For instance, we write

const options = {
  hostname: "example.com",
  path: "/somePath.php",
  method: "GET",
  headers: { Cookie: "myCookie=myvalue" },
};
let results = "";
const req = http.request(options, (res) => {
  res.on("data", (chunk) => {
    results += chunk;
  });
  res.on("end", () => {});
});

req.on("error", (e) => {});

req.end();

to call http.request with the options object.

In it, we set the Cookie header to the keys and values for the cookie.

By John Au-Yeung

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

Leave a Reply

Your email address will not be published.