最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get request headers in azure functions NodeJs Http Trigger - Stack Overflow

programmeradmin0浏览0评论

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
Add a comment  | 

4 Answers 4

Reset to default 9

Use 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.

发布评论

评论列表(0)

  1. 暂无评论