tibsar tibsar - 1 year ago 172
Node.js Question

Getting status, body, and headers from node-libcurl

I have the following code in a Node.js application:

var curlStatus = "";
var curlBody = "";
var curlHeaders = "";
var curlInfo = curl.on('end', function( statusCode, body, headers){
curlStatus = statusCode;
curlBody = body;
curlHeaders = headers;

return {status: curlStatus, body: curlBody, headers: curlHeaders};

curl.on('error', function(){
console.log("CURL ERROR");


Placing a breakpoint on
return {status: curlStatus, body: curlBody, headers: curlHeaders};
shows that the
, and
are being successfully populated. However, putting a breakpoint after
shows that the
, and
variables are still empty strings. How do I pass the information to the parent function?

Answer Source

Welcome to javascript and asynchronous problem.

When you do curl.perform();, the request will start but isn't yet done, that why your variable curlX are not yet populated.
Once the request is done, the callback that you defined with url.on('end', function... is called, and you will populate these variables.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download