【问题标题】:NodeJS Request BearerNodeJS 请求承载
【发布时间】:2017-04-20 23:33:42
【问题描述】:

使用带有 node.js 的请求包我似乎无法得到正确的响应。根据 npm 上的文档,我尝试了几种不同的展示位置,但始终得到相同的结果。

"无效的访问令牌 401"

我在论坛上询问过,他们确保我使用的令牌是正确的。任何帮助将不胜感激!

const restify = require('restify');
const request = require('request');

var server = restify.createServer();
server.listen(process.env.port || process.env.PORT || 3978, () => {
   console.log('%s listening to %s', server.name, server.url);
});


var options = {
  url: 'https://css.api.hp.com/productWarranty/v1/queries',
  json: true,
  method: 'POST',
  Authorization: 'Bearer MYAUTHORIZATIONKEY',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'text/plain',
  }
};

var callback = (error, response, body) => {
  console.log(body);
  console.log(response.statusCode);
}

request(options, callback);

【问题讨论】:

    标签: node.js npm http-post


    【解决方案1】:

    授权元素应该在 headers 对象中。像这样:

    var options = {
      url: '<your url>',
      method: 'POST',
      json: requestBody,
      headers: {
        'User-Agent': 'my request',
        'Authorization': 'Bearer <your token>',
        'Content-Type': 'application/json',
        'Accept': 'application/json'
      }
    };
    

    请试一试。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-19
      • 2017-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多