Node / request keep redirect to original url without returning response

I am using request to check if a given url is corrupted. However, I ran into a weird situation where one given url redirects to itself and the request doesn't return any response. But when I open the browser url it returns a 200 status code.

Does anyone know why the request gets caught in a redirect loop and can't get a response while looking up the url in the browser? How to deal with this problem? Thank!

request({
        uri: 'http://testurl'
        }, function (error, response, body) {
        ......
        }
})

      

Below is the result after setting "request.debug = true"

REQUEST { uri: 'http://testurl',
  callback: [Function],
  tunnel: false }
REQUEST make request http://testurl
REQUEST onResponse http://testurl 302 { 'x-cnection': 'close',
  date: 'Wed, 12 Nov 2014 23:59:22 GMT',
  'transfer-encoding': 'chunked',
  location: 'http://testurl',
  ......,
  'x-powered-by': 'Servlet/2.5 JSP/2.1' }
REQUEST redirect http://testurl
REQUEST redirect to http://testurl
REQUEST {}
REQUEST make request http://testurl
REQUEST response end http://testurl 302 { 'x-cnection': 'close',
  date: 'Wed, 12 Nov 2014 23:59:22 GMT',
  'transfer-encoding': 'chunked',
  location: 'http://testurl',
  ......,
  'x-powered-by': 'Servlet/2.5 JSP/2.1' }
REQUEST onResponse http://testurl 302 { 'x-cnection': 'close',
......

      

UPDATE: After reading the documentation for the request, I realized that it might have to do with cookies. So I add the jar: true request and finally it works.

+3


source to share


1 answer


Try using these request parameters as described in their documentation: - followAllRedirects - track no response GET HTTP 3xx responses as redirects (default: false) - maxRedirects - maximum number of redirects to be done (default: 10)

request({
    uri: 'http://testurl',
    followAllRedirects: true,
    maxRedirects: 50 // some arbitrary value greater than 10
    }, function (error, response, body) {
    ......
    }
})

      



Is the example a public URL? I would like to see if this is the case.

0


source







All Articles