【问题标题】:Get request headers in azure functions NodeJs Http Trigger在天蓝色函数NodeJs Http Trigger中获取请求标头
【发布时间】:2021-03-28 12:17:43
【问题描述】:

如何在 Azure 函数中获取请求标头?我使用 JavaScript http 触发器来处理请求。我需要从前端读取请求标头中发送的一些令牌。我该怎么做?

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();
};

【问题讨论】:

  • 您可以发布您的代码以便我们查看并帮助您吗?

标签: javascript node.js azure http azure-functions


【解决方案1】:

使用req.headers,例如

module.exports = function (context, req) {
    context.log('Header: ' + req.headers['user-agent']);
    context.done();
};

【讨论】:

  • 非常感谢。我在文档的任何部分都找不到。
  • 请注意,标题是小写的,即user-agent而不是User-Agent
【解决方案2】:

您也可以使用运行时 context 执行类似的操作。

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();
};

【讨论】:

    【解决方案3】:

    如果有人想要 C#:

    例如 获取授权令牌:

    log.Info(req.Headers.Authorization.Token.ToString());
    

    更多关于各种标题here

    【讨论】:

      猜你喜欢
      • 2018-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      相关资源
      最近更新 更多