我在这里设置了测试终点:
https://9wi46s5jzc.execute-api.us-east-1.amazonaws.com/test
对于一些基准测试,我转到apitester.com并运行两个测试:
首先,对https://admin:password@9wi46s5jzc.execute-api.us-east-1.amazonaws.com/test(正确的凭据)的发帖请求为我提供了以下输出:
{"isBase64Encoded":false,"statusCode":401,"headers":{"x-powered-by":"Express","content-type":"text/html; charset=utf-8","content-length":"0","etag":"W/\"0-2jmj7l5rSw0yVb/vlWAYkK/YBwk\""},"body":""}
第二,对https://admin:BADPASSWORD@9wi46s5jzc.execute-api.us-east-1.amazonaws.com/test(不正确的凭据)的发布请求使我得到以下输出:
{"message":"Unauthorized"}
因此,它们是应该进行的基准测试。
当我运行以下代码时:
const request = require('request');
const url = 'https://admin:password@9wi46s5jzc.execute-api.us-east-1.amazonaws.com/test';
request.post(url, function(err, res, body) {
console.log("body", body);
});
我得到:
body {"message":"Unauthorized"}
为什么会这样?
根据文档:
https://github.com/request/request
这是进行基本身份验证的方式。
因此,我希望获得正确的授权,但我没有得到授权。我在做什么错了?
答案 0 :(得分:0)
您应该使用以下方法进行尝试:
const proxyUrl = 'https://admin:password@9wi46s5jzc.execute-api.us-east-1.amazonaws.com/test';
const proxyRequest = request.defaults({ 'proxy': proxyUrl});
const options = {
url: '...',
headers: {
"Content-Type": "application/x-www-form-urlencoded",
"Authorization": "Bearer " + token // if this is how you handle auth and you already have the token
}
};
proxyRequest .get(options, function (error, response, body) {
if (error) {
next(error); // if you're using Express
}
console.log("body", body);
});