0

Situation

From my Meteor.js website I'm calling my own REST service. Here's a code sample from my server side

function (question) {   
    var r = Async.runSync(function (done) {
        HTTP.get(URL, {
            params: {q: question}, headers: {
                "Accept": "application/json",
            }
        }, function (err, result) {
            done(err, result);
        });
    });
    if (r.err) {
        console.log("Failed to smartSearch ... ", r.err);
        return null;
    } else if (r.result.content) {
        console.log("Success ... ");
        return JSON.parse(r.result.content);
    }
}

This works great but there is also some crucial information in the response headers which I'm unable to find.

What I've tried so far

But still not seeing my response headers.

Additional Information

I'm fairly new to Meteor.js so I don't really have an idea what I might be doing wrong but getting response headers doesn't see like a strange thing to me.

Community
  • 1
  • 1
Glenn Van Schil
  • 1,059
  • 3
  • 15
  • 33

1 Answers1

1

There is no need to wrap the request as an async call, as it already is.

You can use a try..catch block to handle both successful and failed requests.

try {
  var result = HTTP.get(...);
  var responseHeaders = result.headers;
} catch (e) {
  // handle error
}

If the response headers indicate JSON response, it will be parsed and available as result.data. The response will be available as a string in result.content.

More details are available in the HTTP package API docs.

MasterAM
  • 16,283
  • 6
  • 45
  • 66