Node 'request' module: Getting hold of the last response in a 302-redirect

224 views
Skip to first unread message

Harry

unread,
Apr 17, 2016, 12:00:12 AM4/17/16
to nodejs
Hello,

I am using the Node request module from inside the Node shell to submit a login form via an HTTP POST. If the login is successful, the server redirects to a 'home' page (with an HTTP GET).

The documentation for the request module says that any and all redirects happen by default, and this indeed is happening - I can see the redirect in the server log.

However, on the client side, I need to be able to get hold of the (html) response body of the final redirect. (In my present case, there is only 1 redirect to a home page but it obviously could be more than 1 redirects also in the general case.)

Here's my client-side code:

    var request = require('request');
    request
.post('http://localhost:8080/login', {
        form
: {
            user
:'u',
            password
:'p'
       
}},
       
function(err, resp, body) {
            console
.log(body);
   
});


The problem is, the above code prints nothing on the console!

Question: How to print the response of the final (HTTP GET) redirect? Additionally, in case of a chain of redirects, is it possible to retrieve, on the client side, some info at least on the intermediate redirects - eg, at minimum their URLs but, ideally, also their HTTP method types, query parameters, and request bodies?

Further double-checking: If I use curl against my server, I see the html of the final home page just fine:


    $ curl
-L -d 'user=u' -d 'password=p' http://localhost:8080/login
   
<html> ... </html>
    $


Regards,
/HS

PS: I had posted this earlier on SO, but got no response there!

Daniel Rinehart

unread,
Apr 17, 2016, 9:30:44 AM4/17/16
to nodejs
Given that you are issuing a post request, you'll need to set some of the other request options. In particular look at: followAllRedirects

--
Job board: http://jobs.nodejs.org/
New group rules: https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups "nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nodejs+un...@googlegroups.com.
To post to this group, send email to nod...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nodejs/00eff75c-e416-48dc-a859-b44bf0998ac0%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Harry

unread,
Apr 17, 2016, 11:44:23 AM4/17/16
to nodejs
That was good suggestion, since the flag is set to false by default.

request.post({
  uri
: 'http://localhost:8080/login',
  followAllRedirects
:true,
  form
:{

    user
:"u",
    password
:"p"
 
}},
 
function(err, resp, body) {
    console
.log(body)
});


But even setting it to true (as above) made no difference.

As an aside: In curl too, an explicit use of -X POST does not go down well with a subsequent GET redirect, so I had to use only an implicit POST (that happens when you use the -d 'param=value' options) - this is a known issue with some versions of curl. Not sure if something 'similar' could be going on with the explicit call request.post(...)., or if I'm still missing something. (I sure realize that request and curl are totally different codebases, so just guessing wildly here. I'm not an HTTP expert either.)
Reply all
Reply to author
Forward
0 new messages