Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Node.js中,为什么';请求包是否执行基本身份验证?_Node.js_Request - Fatal编程技术网

在Node.js中,为什么';请求包是否执行基本身份验证?

在Node.js中,为什么';请求包是否执行基本身份验证?,node.js,request,Node.js,Request,我在这里设置了一个测试终点: 对于一些基线测试,我访问apitester.com并运行两个测试: 首先,对:(正确凭证)的post请求为我提供以下输出: {"isBase64Encoded":false,"statusCode":401,"headers":{"x-powered-by":"Express","content-type":"text/html; charset=utf-8","content-length":"0","etag":"W/\"0-2jmj7l5rSw0yVb/vl

我在这里设置了一个测试终点:

对于一些基线测试,我访问apitester.com并运行两个测试:

首先,对:(正确凭证)的post请求为我提供以下输出:

{"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":""}
{"message":"Unauthorized"}
第二,对:(不正确的凭证)的post请求为我提供了以下输出:

{"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":""}
{"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"}
为什么会这样

根据文件:

这是进行基本身份验证的方法


所以我希望得到正确的授权,但我没有得到。我做错了什么?

您应该使用以下方法进行尝试:

        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);
            });

我不太懂这个代码。只有一个端点。“url”应该是什么?