HTTP身份验证请求始终返回401

时间:2017-12-06 22:18:29

标签: http authentication post get request

我想向服务器发出请求,但总是得到401.它可能是什么?

如果我在Postman中尝试一切正常。

request({url: 'https://' + username + ':' + password + '@api.fastspring.com/accounts/'}, function (error, response, body) {
    if(response.statusCode == 200){
        console.log('it worked!')
       res.writeHead(200, { "Content-Type": "text/html" });
       res.write("it worked", 'utf-8');
       res.end();
    } else {
        console.log('error: '+ response.statusCode)
        console.log('error Text: '+ error)
        console.log(body)
        res.writeHead(500, { "Content-Type": "text/html" });
        res.write("Error!", 'utf-8');
        res.end();
    }
});

1 个答案:

答案 0 :(得分:0)

401无法访问'。因此,身份验证正常,您的凭据是正确的(否则您将拥有403),但服务器拒绝您在这些条件下访问此资源。

找出原因的最简单方法是检查服务器日志,但这可能无法实现。以下列出了当您从其他地方使用这些凭据成功访问URL时可能会被拒绝的原因列表。

  • 您正在使用其他请求类型 - 例如POST vs GET。也许只允许一个。
  • 您正在从其他IP地址进行访问。可能服务器也限制通过IP和凭证进行访问。
  • 您的网址略有不同。有些服务器对于尾随/或明确添加/index.html或类似服务器很挑剔。
  • 在这种情况下服务器需要https,但您使用的是http
  • 服务器需要一个Host标头,该标头由一个客户端添加,而不是由另一个客户端添加。这可能发生在具有多个vhost或某些Java应用程序的服务器上
  • 您不了解的其他一些访问规则

确定问题的最佳方法是检查服务器日志。

相关问题