如何将此 cURL 请求转换为节点请求调用?



我已经尝试了我能想到的所有方法,将这个 cURL 请求格式化为我可以在节点上执行的服务器端操作,并且不断返回无效的凭据响应。我知道信条是有效的,所以我必须得出结论,代码发出了错误类型的请求。

cURL 请求:

curl --request GET 
--url https://api.timekit.io/v2/bookings
--header 'Content-Type: application/json' 
--user : api_key_dfagafrgaegrfareVhROys9V1bUJ1z7

我的格式:

var options = {
url: 'https://api.timekit.io/v2/bookings',
headers:{
"Content-Type": "application/json",
'user': APP_KEY
},
method:'GET'
};
function callback(error, response, body) {
if (!error && response.statusCode == 200) {
console.log(body);
}
}
request(options, callback);

我想你的意思是在你的卷曲命令中--user user:password

当您将 --user 传递给 curl 时,它会将其作为基本授权标头发送到服务器(使用 base64 对 user:password 进行编码(。您可以通过针对 httpbin.org 运行它来尝试此操作:

curl --request GET --url http://httpbin.org/headers --header 'Content-Type: application/json' --user foo:bar
{
"headers": {
"Accept": "*/*",
"Authorization": "Basic Zm9vOmJhcg==",
"Connection": "close",
"Content-Type": "application/json",
"Host": "httpbin.org",
"User-Agent": "curl/7.61.1"
}
}

如您所见,他们收到了标头"Authorization": "Basic Zm9vOmJhcg=="其中Zm9vOmJhcg==是 base64 编码foo:bar

最新更新