简体   繁体   English

非常简单的 API 请求使用 Request.js 失败,使用 Curl

[英]Very simple API request fails with Request.js, works with Curl

Simple query to an API, the curl request works, but fails when I try to utilize the request NPM module to perform the request.对 API 的简单查询,curl 请求有效,但是当我尝试使用请求 NPM 模块执行请求时失败。

Curl: Curl:

curl "https://api.squarespace.com/1.0/commerce/orders?modifiedAfter=2019-11-01T12:00:00Z&modifiedBefore=2019-11-15T12:00:00Z" \
  -H "Authorization: Bearer abcde"

Request:要求:

  const response = await request({
     method: 'GET',
     uri:
        'https://api.squarespace.com/1.0/commerce/orders?modifiedAfter=2019-11-01T12:00:00Z&modifiedBefore=2019-11-15T12:00:00Z',
     headers: {
        Authorization: 'Bearer abcde'
     }
  });

Request succeeded with curl, immediate error with request js reporting status code 400. Can you tell me what is amiss with my request usage, and how it even differs from the request generated by curl?请求通过 curl 成功,请求 js 报告状态代码 400 立即出错。你能告诉我我的请求使用有什么问题吗?它与 curl 生成的请求有何不同?

Latest version of request is being used.正在使用最新版本的请求。

The key in your request options must be 'url' and not 'uri' And don't forget to set the callback function that wille be called (so you will also see the error returned when there is one)您的请求选项中的键必须是 'url' 而不是 'uri' 并且不要忘记设置将被调用的回调 function (所以当有一个时你也会看到返回的错误)

const request = require('request');

const options = {
  url: 'https://api.squarespace.com/1.0/commerce/orders?modifiedAfter=2019-11-01T12:00:00Z&modifiedBefore=2019-11-15T12:00:00Z',
  headers: {
     'Authorization': `Bearer abcde`
  }
};

function callback(error, response, body) {
  console.error('error:', error); 
  console.log('statusCode:', response && response.statusCode); 
  console.log('body:', body); 
}

request(options, callback);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM