NPM Request VS Atmosphere HTTP

Hi guys,
Can someone explain difference between Atmosphere HTTP and NPM Request?
Which one is better?
When use Atmosphere version and when use NPM version?

The HTTP package is a wrapper package for the request module, it was probably made to allow easy usage on the client.

If you’re not already using the HTTP package and are using 1.3 then I see no reason to use it instead of request (besides nicer API…)

2 Likes

Thanks @reoh, so I decide to use NPM request instead of Atmosphere HTTP package based on you reply.

Can you explain how to use request package in Meteor 1.3?
I want to use it synchronous but I don’t know what is the best way. I read this guide article but need help to implement a simple request.

This is my code now but it seems still async call:

    import request from 'request';

    let result = {success: false, error: null, content: ""};
    let requestOptions = {
        url: <MY-URL-HERE>
    };

    request(requestOptions, Meteor.bindEnvironment((error, response, html) => {
        if (!error && response.statusCode == 200) {
            result.success = true;
            result.content = html;
            result.error = null;
        }
        else {
            result.success = false;
            result.content = "";
            result.error = error;
        }
    }));

Ah yeah, forgot about that… you need to use either the Meteor.wrapAsync or async/await API because bindEnvironment still runs async. They’re mentioned on that page.

import request from 'request';

// because request callback returns 3 arguments instead of 2
let func = function (options, callback) {
  request(options, function (error, response, body) {
    callback(error, {response, body})
  });
}

syncRequest = Meteor.wrapAsync(func);

let {response} = syncRequest(requestOptions);

if (response.statusCode == 200) {
  response.success = true;
  response.content = html;
  response.error = null;
}
else {
  response.success = false;
  response.content = "";
  response.error = error;
}
1 Like

Thanks @reoh It’s very useful and I learn a lot from your answer (especially wrapAsync part). :wink:
But I have a small issue with you code, It doesn’t return values I also add some console.log like below but view output under the code:

// because request callback returns 3 arguments instead of 2
let func = function (options, callback) {
    request(options, function (error, response, body) {
        console.log("error: " + JSON.stringify(error));
        console.log("response: " + JSON.stringify(response));
        console.log("body: " + JSON.stringify(body));
        callback(error, {response, body})
    });
};

syncRequest = Meteor.wrapAsync(func);

let {response} = syncRequest(requestOptions);

console.log("response: " + JSON.stringify(response));

if (response.statusCode == 200) {
    response.success = true;
    response.content = html;
    response.error = null;
}
else {
    response.success = false;
    response.content = "";
    response.error = error;
}

and this is my output:

I20160518-08:24:22.180(4.5)? error: {}
I20160518-08:24:22.181(4.5)? response: undefined
I20160518-08:24:22.182(4.5)? body: undefined
W20160518-08:24:22.839(4.5)? (STDERR) TypeError: The header content contains invalid characters
W20160518-08:24:22.839(4.5)? (STDERR)     at Object.Future.wait (/home/cyc/.meteor/packages/meteor-tool/.1.3.2_4.7bk6xv++os.linux.x86_64+web.browser+web.cordova/mt-os.linux.x86_64/dev_bundle/server-lib/node_modules/fibers/future.js:420:15)
W20160518-08:24:22.839(4.5)? (STDERR)     at packages/meteor/helpers.js:119:1
W20160518-08:24:22.839(4.5)? (STDERR)     at Object.getContent (server/lib/get_content.js:51:26)
W20160518-08:24:22.839(4.5)? (STDERR)     at Object.fetch (server/lib/get_content.js:205:27)
W20160518-08:24:22.839(4.5)? (STDERR)     at Object.fetchSource (server/lib/get_content.js:369:31)
W20160518-08:24:22.840(4.5)? (STDERR)     at [object Object].action (server/controller/postsController.js:79:39)
W20160518-08:24:22.840(4.5)? (STDERR)     at [object Object].handle (packages/iron_middleware-stack/lib/handler.js:74:1)
W20160518-08:24:22.840(4.5)? (STDERR)     at boundNext (packages/iron_middleware-stack/lib/middleware_stack.js:251:1)
W20160518-08:24:22.840(4.5)? (STDERR)     at runWithEnvironment (packages/meteor/dynamics_nodejs.js:110:1)
W20160518-08:24:22.840(4.5)? (STDERR)     at packages/meteor/dynamics_nodejs.js:123:1
W20160518-08:24:22.840(4.5)? (STDERR)     - - - - -
W20160518-08:24:22.840(4.5)? (STDERR)     at ClientRequest.OutgoingMessage.setHeader (http.js:733:13)
W20160518-08:24:22.840(4.5)? (STDERR)     at new ClientRequest (http.js:1429:14)
W20160518-08:24:22.840(4.5)? (STDERR)     at Object.exports.request (http.js:1899:10)
W20160518-08:24:22.841(4.5)? (STDERR)     at Request.start (/home/cyc/Programming/Projects/content/Sources/node_modules/request/request.js:753:32)
W20160518-08:24:22.841(4.5)? (STDERR)     at Request.end (/home/cyc/Programming/Projects/content/Sources/node_modules/request/request.js:1418:10)
W20160518-08:24:22.841(4.5)? (STDERR)     at end (/home/cyc/Programming/Projects/content/Sources/node_modules/request/request.js:580:14)
W20160518-08:24:22.841(4.5)? (STDERR)     at Object._onImmediate (/home/cyc/Programming/Projects/content/Sources/node_modules/request/request.js:594:7)
W20160518-08:24:22.841(4.5)? (STDERR)     at processImmediate [as _immediateCallback] (timers.js:363:15)

@reoh Can you explain async/await way too?? It’s interesting for me to see both solution for this problem.

@reoh I test a lot and find that above error message accord only when request contain error. I think maybe with some hack, above code can work in all situation.