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.

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

so how did you fix it?
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.

7 Comments

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.
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.
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.
Have you checked/waited for a timeout ?
This could be an SSL issue at your end aswell. Have you tried a simple http request ? (non https)
|
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();
        });
    });
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.