jojo oresanya
jojo oresanya

Reputation: 59

How do I set Authorization Bearer header with nodejs

I am working on a signup page and I am lost trying to set the Authorization Bearer Header. I am using jsonwebtokens to generate the token. I know how to set the header on postman, but how do I set it for the actual route I’m signing up to and be able to use it in my auth middleware for other endpoints ? As postman is just for tests

Upvotes: 2

Views: 15324

Answers (2)

Solomon Gbadamosi
Solomon Gbadamosi

Reputation: 121

request.setHeader('Authorization', 'Bearer '+accessToken)

Upvotes: 3

xMayank
xMayank

Reputation: 1995

you can set headers inside request in your route. Using request module.

app.post('/test', (req, res, next) => {
    console.log("inside the test");
    request.post({
      url: api_address,
      body: JSON.stringify(send),
      headers: {
         "Content-Type":"application/json",
         "Authorization": "Bearer 71D50F9987529"
}
    }, function (error, response, body) {
      console.log("hiii");
      console.log(response.statusCode);
      if (!error && response.statusCode == 200) {
        // Successful call
        var results = JSON.parse(body);
        console.log(results) // View Results
      }
    });
  });

Upvotes: 2

Related Questions