request.js 失败,其中 CURL 成功获得授权 header 的 GET 请求
request.js fails where CURL succeeds for GET request with Authorization header
我可以使用来自 curl
的授权 header 发出 GET 请求,但不能来自 Node.js 中的 request
或 https
。服务器 returns 状态 200 与 curl 但 500 与 request
或 https
。来自 request
或 https
的呼叫与 curl
有何不同?服务器如何以不同方式读取它们?
以下 cURL 从命令行成功:
curl -H "Authorization: Bearer abc123def456" https://api.domain.com/path/to/resource
但是相同的请求在 Node
中失败并显示 request.js
var options = {
type: 'get',
url: "https://api.domain.com/path/to/resource",
headers: {
"Authorization": " Bearer abc123def456"
}
}
request(options, function (err, response, body) {
assert.equal(response.statusCode, 200) ; // 500 internal error
})
使用 auth
选项的 request.js 以下操作也失败了:
var options = {
type: 'get',
url: "https://api.domain.com/path/to/resource",
auth: {
"bearer": "abc123def456"
}
}
request(options, function (err, response, body) {
assert.equal(response.statusCode, 200) ; // 500 internal error
})
在不使用 request.js
的情况下使用 https
时也会失败:
var options = {
host: 'api.domain.com',
port: 443,
path: '/path/to/info',
method: 'GET',
headers: {
"Authorization": " Bearer abc123def456"
}
}
var req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('end', function () {
assert.equal(res.statusCode, 200) // 500 internal error
})
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
但如果从 Node 中脱壳,curl 请求会成功:
exec("curl -H "Authorization: Bearer abc123def456" https://api.domain.com/path/to/resource", function (error, stdout, stderr) {
var obj = JSON.parse(stdout) // successfully retrieved and parsed
});
request-debug
提供以下信息:
{ request:
{ debugId: 1,
uri: 'https://api.domain.com/path/to/resource',
method: 'GET',
headers:
{ host: 'api.domain.com',
authorization: 'Bearer abc123def456' } } }
500 internal error一般表示服务器端有错误。因此,理想情况下,您应该查看服务器日志。
但是,如果您无权访问这些日志,请查看您尝试过的每个选项发送的请求之间的差异:
模块:请求(使用手动指定的身份验证header):
GET /path/to/resource HTTP/1.1
Authorization: Bearer abc123def456
host: api.domain.com
模块:请求(具有明确指定的身份验证header):
GET /path/to/resource HTTP/1.1
host: api.domain.com
authorization: Bearer abc123def456
模块:HTTP(手动指定身份验证 header):
GET /path/to/info HTTP/1.1
Authorization: Bearer abc123def456
Host: api.domain.com
卷曲:
GET /path/to/resource HTTP/1.1
Host: api.domain.com
User-Agent: curl/7.51.0
Accept: */*
Authorization: Bearer abc123def456
很明显,其余模块不发送 HTTP headers 'User-Agent' 和 'Accept'。因此,可能是服务器上的应用程序 运行 试图解析其中的至少一个但失败了。
我可以使用来自 curl
的授权 header 发出 GET 请求,但不能来自 Node.js 中的 request
或 https
。服务器 returns 状态 200 与 curl 但 500 与 request
或 https
。来自 request
或 https
的呼叫与 curl
有何不同?服务器如何以不同方式读取它们?
以下 cURL 从命令行成功:
curl -H "Authorization: Bearer abc123def456" https://api.domain.com/path/to/resource
但是相同的请求在 Node
中失败并显示 request.jsvar options = {
type: 'get',
url: "https://api.domain.com/path/to/resource",
headers: {
"Authorization": " Bearer abc123def456"
}
}
request(options, function (err, response, body) {
assert.equal(response.statusCode, 200) ; // 500 internal error
})
使用 auth
选项的 request.js 以下操作也失败了:
var options = {
type: 'get',
url: "https://api.domain.com/path/to/resource",
auth: {
"bearer": "abc123def456"
}
}
request(options, function (err, response, body) {
assert.equal(response.statusCode, 200) ; // 500 internal error
})
在不使用 request.js
的情况下使用 https
时也会失败:
var options = {
host: 'api.domain.com',
port: 443,
path: '/path/to/info',
method: 'GET',
headers: {
"Authorization": " Bearer abc123def456"
}
}
var req = https.request(options, function (res) {
res.setEncoding('utf8');
res.on('end', function () {
assert.equal(res.statusCode, 200) // 500 internal error
})
});
req.on('error', function (e) {
console.log('problem with request: ' + e.message);
});
req.end();
但如果从 Node 中脱壳,curl 请求会成功:
exec("curl -H "Authorization: Bearer abc123def456" https://api.domain.com/path/to/resource", function (error, stdout, stderr) {
var obj = JSON.parse(stdout) // successfully retrieved and parsed
});
request-debug
提供以下信息:
{ request:
{ debugId: 1,
uri: 'https://api.domain.com/path/to/resource',
method: 'GET',
headers:
{ host: 'api.domain.com',
authorization: 'Bearer abc123def456' } } }
500 internal error一般表示服务器端有错误。因此,理想情况下,您应该查看服务器日志。
但是,如果您无权访问这些日志,请查看您尝试过的每个选项发送的请求之间的差异:
模块:请求(使用手动指定的身份验证header):
GET /path/to/resource HTTP/1.1
Authorization: Bearer abc123def456
host: api.domain.com
模块:请求(具有明确指定的身份验证header):
GET /path/to/resource HTTP/1.1
host: api.domain.com
authorization: Bearer abc123def456
模块:HTTP(手动指定身份验证 header):
GET /path/to/info HTTP/1.1
Authorization: Bearer abc123def456
Host: api.domain.com
卷曲:
GET /path/to/resource HTTP/1.1
Host: api.domain.com
User-Agent: curl/7.51.0
Accept: */*
Authorization: Bearer abc123def456
很明显,其余模块不发送 HTTP headers 'User-Agent' 和 'Accept'。因此,可能是服务器上的应用程序 运行 试图解析其中的至少一个但失败了。