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();
};
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();
};
Share
Improve this question
edited Dec 19, 2017 at 16:33
Jewel Lukose
asked Dec 19, 2017 at 15:54
Jewel LukoseJewel Lukose
701 gold badge1 silver badge7 bronze badges
1
- can you post your code so we can look at it and help you out? – hjm Commented Dec 19, 2017 at 15:56
4 Answers
Reset to default 9Use req.headers
, e.g.
module.exports = function (context, req) {
context.log('Header: ' + req.headers['user-agent']);
context.done();
};
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();
};
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')
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.