Programmix
Programmix

Reputation: 116

Node.JS Request Module Callback Not Firing

I'm running this code using the request module for node.js

var hsKey    = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var hsForm   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
var hsHost   = "https://docs.google.com/"
var url = hsHost + "forms/d/" + hsForm + "/formResponse"

var form = {
    "entry.129401737": pointsAvg,
    "entry.2000749128": hiddenNeurons,
    "submit": "Submit",
    "formkey": hsKey
};

request.post({
    url: url,
    form: form
}, function (err, res, body) {
    console.log("Sent data");
});

I have tried running the above code just using standard Node.JS libraries, to no avail. The callback function is never fired and the request doesn't go through. I don't know why.

Upvotes: 2

Views: 2148

Answers (3)

jsaddwater
jsaddwater

Reputation: 1829

I ran into this as well. I ended up creating a separate js file containing only the request, without the describe and it methods, and running it with 'mocha mynewbarebonesreq.js'. suddenly I could see that there was an exception being thrown and swallowed by mocha (with the standard reporter, spec).

I finally installed and enabled mocha_reporter which shows the exceptions

now it looks like this:

describe('CMSLogin', function () {
    it('should log in as user ' + JSON.stringify(USER_PASS), function (done) {
        request({
            url: "http://cms.lund.multiq.com:3000/api/CMSUsers/login",
            method: "POST",
            headers: {
                'Content-Type': 'application/json',
                'Accept': 'application/json',
            },
            json: false,
            body: JSON.stringify(USER_PASS)
        }, (err, res, body) => {
            var parsedBody = JSON.parse(body);
            this.token = parsedBody.id; 
            console.log(this.token)
            assert.equal(USER_PASS.userId, parsedBody.userId);
            assert.doesNotThrow(() => Date.parse(parsedBody.created));
            if (err) { done.fail(err); }

            done();
        });
    });
}

Upvotes: 0

Programmix
Programmix

Reputation: 116

I believe I've found the answer to my own problem. The issue seems to be that I'm not allocating any time in the Node.js event loop to allow the request to be executed.

Upvotes: 2

Aukhan
Aukhan

Reputation: 481

Have a look at this question:

your code should look something like

var hsKey    = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var hsForm   = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
var hsHost   = "https://docs.google.com/"
var url = hsHost + "forms/d/" + hsForm + "/formResponse"

var form = {
  "entry.129401737": pointsAvg,
  "entry.2000749128": hiddenNeurons,
  "submit": "Submit",
  "formkey": hsKey
};

request.post({
    url: url,
    form: form
}, function (response) {
      response.setEncoding('utf8');
      response.on('data', function(chunk){
          //do something with chunk
      });
});

The data event should get fired on receiving a response.

So if you read the docs for the request module at npm

request
    .get('http://google.com/img.png')
    .on('response', function(response) {
         console.log(response.statusCode) // 200 
         console.log(response.headers['content-type']) // 'image/png' 
     });

There is a response event that should get fired.

Upvotes: 0

Related Questions