Neekoy Neekoy - 1 year ago 127
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'\?format\=json

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

var request = require('request');


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

Any help is appreciated.

Answer Source
  • 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:

  url     : url,
  method  : 'GET',
  headers : {
    Authorization : 'ApiKey myUser:verySecretAPIKey',
    Accept        : 'text/json'
  }, function (error, response, body) {
    if (error) throw error;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download