Search code examples
meteor

Translating curl command to Meteor's HTTP call


I try to "translate" this curl command

curl --request POST --header "Content-Type: application/json" --url http://some-url --user userName:apiKey --data '{ "some": "JSON data as string" }'

into Meteor's HTTP call. I tried this:

const options = {
  { "some": "JSON data as object" },
  headers: {
    'Content-Type': 'application/json',
  },
  params: {
    user: 'userName:apiKey',
  },
  // Or alternatively
  //
  // user: 'userName:apiKey',
};

HTTP.call('POST', 'http://some-url', options, (error, result) => {
  if (error) {
    reject(error);
  } else {
    resolve(result);
  }
});

With curl command it works fine, with HTTP.call I get a 403, Forbidden. Authorization with userName:apiKey seems to fail. How do I specify the userName:apiKey in the HTTP.call example? Or maybe their is another problem?


Solution

  • If you need authentication, you need to add the auth parameter. The params parameter will actually set all the containing properties to be part of the POST request body.

    const options = {
      data: { "some": "JSON data as object" },
      headers: {
        'Content-Type': 'application/json',
      },
      auth: 'userName:apiKey'
    }
    

    Read: https://docs.meteor.com/api/http.html#HTTP-call