sincity sincity - 3 months ago 19
HTTP Question

HTTP request callback not executing - why?

I'm attempting to submit an HTTP POST request to a 3rd-party API. It's currently wrapped in a promise method.

The callback function is never executed, however when I run the code below just by itself, the call to the API is successful.

I'm fairly new to Node.js and the idea of an event-loop. What exactly is happening behind the scenes and why does the callback function not execute?

Code:

// SQL Query successfully called and passed into done() function
.done(function(sqlResult) {

// contact already exists. resolve the issue
if(sqlResult[0]) {
deferred.resolve("Contact already exists in database.");
}

// no contact found. create new HubSpot contact
else {
console.log("No contact found. Creating HubSpot contact...");

// create the POST data object
var postData = querystring.stringify({
'email': contact.email,
'firstname': contact.first_name,
'lastname': contact.last_name,
'hs_context': JSON.stringify({
'hutk': contact.cookie,
'ipAddress': hs_context.ipAddress,
'pageUrl': hs_context.pageUrl,
'pageName': hs_context.pageName
})
}),

// set POST options
options = {
hostname: 'forms.hubspot.com',
path: '/uploads/form/v2/xxxxxx/' + formID,
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'Content-Length': postData.length
}
};

// set up request
console.log("Setting up HubSpot API request.");
var request = http.request(options, function(response) {
// THIS CALLBACK DOES NOT EXECUTE
console.log("TEST - INSIDE REQUEST CALLBACK.");
console.log("Status: " + response.statusCode);
console.log("Headers: " + JSON.stringify(response.headers));
response.setEncoding('utf8');
response.on('data', function(chunk) {
console.log("Body: " + chunk);
});
});

// handle any errors
request.on('error', function(e) {
console.log("Problem with request - " + e);
});

// Post the data
console.log("Posting data.");
request.write(postData);
request.end();
} // end of else

...


Output:

No contact found. Creating HubSpot contact...
Setting up HubSpot API request.
Posting data.


Any suggestions? I received the code from Hubspot's API docs - http://developers.hubspot.com/docs/methods/forms/submit_form - and tweaked to fit my needs.

Update:

I've noticed several of my tests have succeeded in making the API call. None of the
console.log()
are displaying, but when I go to the 3rd-party website I see every now and then the API call is successfully made.

I've made a quick time stamp at when I ran my tests.

12:09:12 - Unsuccessful
12:10:45 - Unsuccessful
12:11:05 - Unsuccessful
12:11:14 - Unsuccessful
12:11:32 - Successful!


I'm still struggling to understand how this can happen ...

Answer

I failed to mention I am using Mocha for testing. The testing code did not have describe/it wrapped around it, so the code would finish executing before any response is received. I changed my testing code to the following:

describe("Submitting form to HubSpot API", function() {
    it("should return 204 status code", function(done) {
        test.createContact(contact, context, formID)
        .done(function(response) {
            test.log("CreateContact resolved! SUCCESS: " + response);
        })
        .fail(function(response) {
            test.log("CreateContact rejected! ERROR: " + response);
        })
        .always(function() {
            done();
        });
    });
});

And I finally receive a response!