Meteor: Make HTTP calls to remote services

To make HTTP request to remote services, we have to use Meteor.http package.
Meteor.http package can be added to project by command “meteor add http”.

Meteor.http supports both synchronous and asynchronous requests. But in this example/usecase, we want to return results to client. And Meteor by default supports synchronous services on server (which conflicts with node.js asynchronous model), so I will use synchronous Meteor.http api in this example.

var url = 'http://.....';
var result = Meteor.http.get(url, {timeout:30000});
if(result.statusCode==200) {
    var respJson = JSON.parse(result.content);
    console.log("response received.");
    //custom code
    if(respJson.message.code == 0) { //IF OK
        console.log('returning response');
        return respJson;
    } else {
        throw new Meteor.Error(respJson.message.code, respJson.message.text);
    }
} else {
    console.log("Content issue: ", result.statusCode);
    throw new Meteor.Error("");
}

Full source code is available at https://gist.github.com/2922814
Live demo (twitter): http://remoteservice.meteor.com

  • Hello

    How to pull data realtime from service?

  • Vivek_1729

    How can I make an http post request to the meteor server from an android application?

  • Cristiano Matos

    Thank you that was what I needed, very nice post. It seems to me that the example is not working.