Categories
JavaScript Answers

How to use http.client in Node.js if there is basic authorization?

Spread the love

To use http.client in Node.js if there is basic authorization, we call request with the token in the header.

For instance, we write

const username = "Test";
const password = "123";
const auth =
  "Basic " + Buffer.from(username + ":" + password).toString("base64");
const header = { Host: "www.example.com", Authorization: auth };
const request = client.request("GET", "/", header);

to create the basic auth token.

Then we call client.request with the header object which has the Authorization header set to the basic auth token.

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 *