1

Simple query to an API, the curl request works, but fails when I try to utilize the request NPM module to perform the request.

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?

Latest version of request is being used.

Dygerati
  • 646
  • 1
  • 6
  • 16
  • Please try adding `--verbose` option to `curl` to see which headers `curl` sends during request. For example, it's possible that `curl` sends `Accept: */*` but your request with `request` doesn't. – TheJavaGuy-Ivan Milosavljević Sep 24 '20 at 07:39

1 Answers1

0

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)

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);
rom1bl
  • 133
  • 2
  • 6
  • Request documentation shows that both uri and url are supported parameters, and changing that doesn't make a difference. I'm not using a callback since I'm actually using the 'request-promise-native' package with async/await. – Dygerati Nov 15 '19 at 23:54
  • Then i have no idea :/ maybe u could try axios or node-fetch as a last resort – rom1bl Nov 18 '19 at 15:05