Meteor Js : Calling a REST Service

In this example I will show how to call a REST service and parse this results to JSON format.

Example:

Function implementation

var restServiceCall = function (url) {
    var result = HTTP.get(url, {
        headers: parseHeaders,
        params: getParseParams()
    });

    if (result.statusCode == 200) {
        return JSON.parse(result.content).results;
    } else {
        var json = JSON.parse(result.content);
        throw new Meteor.Error(result.statusCode, json.error);
    }
};

Using my function

Now I will have a array of objects in JSON format.

var myArrayObjects = restServiceCall('https://restServiceCall/param/1');

3 Comments

  1. Hi, thanks a lot for your code! 😀
    I’m encountering a problem, and maybe you can help me out a bit >_<

    I tried running this code client-side:
    http://paste.ofcode.org/uH6Y6R5ZYbLmXQeQDhLc27
    but I got the error "Can't make a blocking HTTP call from the client; callback required"

    so I tried running this other code:
    http://paste.ofcode.org/YWjXn2PGK7YmVvwAVxn3Cd
    and instead I get the error "Cannot read property '0' of undefined"

    Any word of advice? 🙂

    Like

  2. I’m really enjoying the design and layout of your website.
    It’s a very easy on the eyes which makes it much more enjoyable for
    me to come here and visit more often. Did you hire out a
    developer to create your theme? Superb work!

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s