NodeJS请求承载

时间:2017-04-20 23:33:42

标签: node.js npm http-post

我似乎无法使用带节点的请求包获得正确的响应。我根据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);

1 个答案:

答案 0 :(得分: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'
  }
};

请试一试。