Request: How can I access the url of the request after a redirect?

Created on 4 Mar 2014  ·  3Comments  ·  Source: request/request

I need to access the url of a request after it has been redirected e.g:

 function(err, resp,body){
            $ = cheerio.load(body);
            //console.log(jar.cookies);
            request.post({
                jar:jar,
                url:targetBaseURL+'/search.php?do=process',
                followAllRedirects: true,
                headers: {
                    'User-Agent': userAgent

                },
                form :{
                    's'             : '',
                    'do'            : 'process',
                    'searchthreadid': '0',
                    'query'         : '',
                    'titleonly'     : '0',

...
                proxy: 'http://127.0.0.1:8888'

            }, function(err, resp, body){
               console.log(resp.request.redirects); // array is empty
                $ = cheerio.load(body);
                $("[id*=post]").each(function(i, elem){

                    var title =$(elem).find('a strong');
                    if(!$(title).text().match(/livescores/i)) {
                        //console.log($(title).text().trim());
                        var post = $(elem).find('div > em');
                        //console.log($(post).text().trim());
                    }
                });

            })
        });

Most helpful comment

This gives me what I need console.log(resp.request.uri.href);

Found here - http://stackoverflow.com/questions/16687618/how-do-i-get-the-redirected-url-from-the-nodejs-request-module

All 3 comments

look in response.request.redirects, it has an array of the redirect urls.

@spollack Thanks. Unfortunately, its an empty array.

This gives me what I need console.log(resp.request.uri.href);

Found here - http://stackoverflow.com/questions/16687618/how-do-i-get-the-redirected-url-from-the-nodejs-request-module

Was this page helpful?
0 / 5 - 0 ratings