REST API consumption


#1

I’m trying to call the IGDB API within a Meteor app. They’ve provided an example for calling it in Node:

unirest.get("https://igdbcom-internet-game-database-v1.p.mashape.com/games/?fields=name&limit=10&offset=0&order=release_dates.date%3Adesc&search=zelda")
.header("X-Mashape-Key", "thisismykey")
.header("Accept", "application/json")
.end(function (result) {
  console.log(result.status, result.headers, result.body);
});

I’m unsure how to make this compatible in Meteor. I’ve tried calling it like:

if (Meteor.isServer) {
	HTTP.call( 'GET', 'https://igdbcom-internet-game-database-v1.p.mashape.com/games/?fields=name&limit=10&offset=0&order=release_dates.date%3Adesc&search=zelda', {}, function( error, response ) {
  if ( error ) {
    console.log( error );
  } else {
    console.log( response );
  }
});
}

but I don’t know how to input my API key in this example.


#2

Untested, but this should work:

if (Meteor.isServer) {
  HTTP.call('GET', 'https://igdbcom-internet-game-database-v1.p.mashape.com/games/?fields=name&limit=10&offset=0&order=release_dates.date%3Adesc&search=zelda', {
    headers: {
      'X-Mashape-Key': 'thisismykey',
      'Accept': 'application/json',
    },
  }, function (error, response) {
    if (error) {
      console.log(error);
    } else {
      console.log(response);
    }
  });
}

Or, as this needs to be run on the server, put the code into a server-only file and remove the isServer test. Also, use sync-style coding and the short-form GET:

try {
  const response = HTTP.get('https://igdbcom-internet-game-database-v1.p.mashape.com/games/?fields=name&limit=10&offset=0&order=release_dates.date%3Adesc&search=zelda', {
    headers: {
      'X-Mashape-Key': 'thisismykey',
      'Accept': 'application/json',
    },
  });
} catch (error) {
  throw new Meteor.Error('oops', 'something bad happened')
}