Reputation: 70
How do I get the request headers in Azure functions ? I use JavaScript http trigger to handle a request. I need to read some token sent in the request header from the front end. How can I do this ?
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
if (true) {
context.log(req.headers['Authorization'])
context.res = {
// status: 200, /* Defaults to 200 */
body: "Hello there "
};
}
else {
context.res = {
status: 400,
body: "Please pass a name on the query string or in the request body"
};
}
context.done();
};
Upvotes: 5
Views: 12969
Reputation: 1024
For those reading this in 2025, using Azure Function model v4 and NodeJS 18: request.headers
currently returns an object with a javascript Map, the latter containing the headers list. To get the "Authorization" header value, use the Map.get() method: request.headers.get('Authorization')
Upvotes: 1
Reputation: 783
You can also do something like this with the run time context also.
module.exports = function (context, req) {
context.log('JavaScript HTTP trigger function processed a request.');
context.log(context.req.headers.authorization)//You can get the pass tokens here
context.done();
};
Upvotes: 4
Reputation: 843
In case anyone wants the C#:
e.g. To get the Authorization token:
log.Info(req.Headers.Authorization.Token.ToString());
More on the various headers here.
Upvotes: 0
Reputation: 35134
Use req.headers
, e.g.
module.exports = function (context, req) {
context.log('Header: ' + req.headers['user-agent']);
context.done();
};
Upvotes: 9