'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
Solution 1:[1]
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
}
});
});
Solution 2:[2]
request.setHeader('Authorization', 'Bearer '+accessToken)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | |
| Solution 2 | Solomon Gbadamosi |
