2

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.

Programmix
  • 116
  • 6

3 Answers3

2

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.

Programmix
  • 116
  • 6
0

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.

Aukhan
  • 481
  • 4
  • 9
  • Sorry if I wasn't very clear. I am using the request module. What you have specified is how to handle incoming data using node.js's default http library, which I am not using in this case. – Programmix Feb 23 '16 at 09:48
  • Did you read the title of the question I referenced ? How to make external HTTP requests with Node.js. Apologies for not relating to the request module itself, but node's inbuilt http is also capable of doing this. – Aukhan Feb 23 '16 at 09:51
  • Regarding the newest edit to your answer: I am not using a GET request; I am making a POST request. Furthermore, I know that the callback should be fired, but the issue that I am presenting here is that the callback is not being fired and the request is not being executed. – Programmix Feb 23 '16 at 10:00
  • Have you checked/waited for a timeout ? – Aukhan Feb 23 '16 at 10:15
  • The request should timeout after a certain interval if it doesn't receive a response, and at that point your callback should get fired giving a timeout error. How long have you waited in the console after running this code ? This according to the docs depends on the OS environment. – Aukhan Feb 23 '16 at 10:22
  • There's never a timeout either. It seems like the request isn't even being sent, though it should be. – Programmix Feb 23 '16 at 10:31
  • 1
    This could be an SSL issue at your end aswell. Have you tried a simple http request ? (non https) – Aukhan Feb 23 '16 at 10:41
0

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();
        });
    });
}
jsaddwater
  • 1,781
  • 2
  • 18
  • 28