Neekoy Neekoy - 5 months ago 21
Node.js Question

cURL call to API in NodeJS Request

it's me again with another lame question. I have the following call to a Rattic password database API which works properly:

curl -s -H 'Authorization: ApiKey myUser:verySecretAPIKey' -H 'Accept: text/json' https://example.com/passdb/api/v1/cred/\?format\=json


I tried to replicate this call in NodeJS, however the following returns blank:

var request = require('request');

url='https://example.com/passdb/api/v1/cred/?format=json';

request({
url: url,
method: 'POST',
headers: [
{ 'Authorization': 'ApiKey myUser:verySecretAPIKey' }
],
},
function (error, response, body) {
if (error) throw error;
console.log(body);
}
);


Any help is appreciated.

Answer
  • As pointed out in the comments already, use GET, not POST;
  • headers should be an object, not an array;
  • You're not adding the Accept header.

All combined, try this:

request({
  url     : url,
  method  : 'GET',
  headers : {
    Authorization : 'ApiKey myUser:verySecretAPIKey',
    Accept        : 'text/json'
  }, function (error, response, body) {
    if (error) throw error;
    console.log(body);
  }
});