【问题标题】:How to convert this cURL request into node request call?如何将此 cURL 请求转换为节点请求调用?
【发布时间】:2018-09-15 06:39:21
【问题描述】:

我已经尝试了所有我能想到的方法来将此 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);

【问题讨论】:

    标签: node.js http curl request


    【解决方案1】:

    我认为您的 curl 命令中的意思是 --user user:password

    当您将 --user 传递给 curl 时,它会将其作为基本授权标头发送到服务器(其中 user:password 使用 base64 编码)。您可以通过在 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==foo:bar 的base64 编码

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 1970-01-01
      • 2017-10-31
      • 2016-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-28
      相关资源
      最近更新 更多