调用第三方API会给我错误:301永久移动

时间:2019-02-21 12:19:45

标签: node.js api request postman

当我使用邮递员调用API时,它会给我成功的响应,但是我在Node js应用程序中尝试过的相同代码会给我类似错误。我是否在服务器端犯了任何错误或错误?我还尝试了邮递员生成的示例代码。但这也会产生错误。对于请求,我同时使用了request-promiserequest两个npm软件包

<html> <head><title>301 Moved Permanently</title></head> <body
bgcolor="white"> <center><h1>301 Moved Permanently</h1></center>
<hr><center>nginx</center> </body> </html>

示例代码:

var options = { 
  method: 'PUT',
  url: 'http://IPAdd/app/v1/device/mac/zz',
  headers: 
   {
     'content-type': 'application/json',
     authorization: 'Token '+ token },
  body: 
   { //body JSON},
  json: true,"rejectUnauthorized": false 
};

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});

1 个答案:

答案 0 :(得分:0)

我需要传递带有请求对象的followAllRedirects: true,才能遵循所有重定向

var options = { 
  method: 'PUT',
  url: 'http://IPAdd/app/v1/device/mac/zz',
  headers: 
   {
     'content-type': 'application/json',
     authorization: 'Token '+ token },
  body: 
   { //body JSON},
  json: true,
  "rejectUnauthorized": false , 
  followAllRedirects: true
};

request(options, function (error, response, body) {
if (error) throw new Error(error);

console.log(body);
});
相关问题