【问题标题】:How do I set Authorization Bearer header with nodejs如何使用 nodejs 设置 Authorization Bearer 标头
【发布时间】:2020-10-25 10:24:15
【问题描述】:

我正在处理注册页面,但我在尝试设置授权承载标头时迷路了。我正在使用 jsonwebtokens 生成令牌。我知道如何在邮递员上设置标头,但是如何为我正在注册的实际路由设置它并能够在我的身份验证中间件中将它用于其他端点?因为邮递员只是为了测试

【问题讨论】:

    标签: node.js authentication header authorization bearer-token


    【解决方案1】:

    您可以在路由的请求中设置标头。使用request 模块。

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

    【讨论】:

    • 我不想在邮递员中这样做,我想直接从节点中的发布路线设置授权
    • 作为一个技巧,您可以使用邮递员为您生成任何语言的代码。
    猜你喜欢
    • 2021-09-30
    • 1970-01-01
    • 2021-03-27
    • 2021-01-01
    • 1970-01-01
    • 2017-11-26
    • 2020-06-10
    • 2017-11-30
    • 2017-09-12
    相关资源
    最近更新 更多