Http.get x-auth-token question


#1

Hi

Consuming an api that requires authentication in the form of 'X-Auth-Token: key’
What is the correct syntax with http.get?

I used params: till I got 429 reaching my request limit, looking at the docs it should be { headers:‘X-Auth-Token : c55249df51e42368a9f2d28cf143507’ } but I get a 500 error and an HTML result.

I’m calling these api-consuming-methods using publish but how do I return errors to the subscriber in cases like the above when the error message isn’t a cursor?

Is there a way to condition the publish/subscribe for 3rd party api errors?


#2

headers is defined as an object containing a dictionary of strings, so the syntax you want is:

headers:{ 'X-Auth-Token' : 'c55249df51e42368a9f2d28cf143507' }

However, headers is a property of options, so would tend to appear in the options object like this:

options = {
  headers: { 'X-Auth-Token' : 'c55249df51e42368a9f2d28cf143507' }
}

#3

Fantastic! now I can also refactor options thank you :grinning:

Regarding publish for api call results? any thoughts?


#4

Assuming you mean how to handle errors? There is a convention for handling server-side errors and passing them over DDP to the client from methods or publications.

Your publish should contain some method of error detection (for example try … catch) and then as its final act should

throw new Meteor.Error(...);

The subscribe on the client needs to be able to pick up the passed error, so instead of a simple

Meteor.subscribe('myCollection');

you would do

Meteor.subscribe('myCollection', {
  onError: function(err) {
    // do something with err here
  }
});