Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a variable that tells you if it redirected or not #6

Closed
datagutt opened this issue Feb 3, 2013 · 5 comments · Fixed by #18
Closed

Add a variable that tells you if it redirected or not #6

datagutt opened this issue Feb 3, 2013 · 5 comments · Fixed by #18

Comments

@datagutt
Copy link

datagutt commented Feb 3, 2013

Would be useful to have a way to know if the url you passed redirected to another or not.

@ignasbernotas
Copy link

In regards to the same issue, is it possible to pass the last redirected URL to the response argument in the callback?
so I could do:

http.request(url1, function(response) {
    console.log(response.currentURL); //or similar. Maybe I just overlooked something?
});

@ignasbernotas
Copy link

I've figured it out.
In the redirectCallback method add this:

      function redirectCallback (reqUrl, redirect) {
        return function (res) {
          //these two following lines:
          res.currentURL = reqUrl; //will have the last redirected url
          res.redirectCount = redirect.count -1; //will include the number of redirects

Therefore the in the request response callback you can fetch them as in the previous comment:

http.request(url1, function(response) {
    console.log(response.currentURL);
    console.log(response.redirectCount);
});

Btw, awesome module!

@elgs
Copy link

elgs commented May 24, 2014

+1 for this.

@elgs
Copy link

elgs commented May 24, 2014

Actually not changing the status code would be good enough, I think. We know the initial url because we make the request, so I don't need other people to tell me my initial request url. If the status code can still be 30x, not changing it to 200, that would be good enough.

@elgs
Copy link

elgs commented May 24, 2014

I found a workaround:

var redirectedUrl = res.headers['content-location'];
console.log(redirectedUrl, originalUrl);
if (redirectedUrl && redirectedUrl !== originalUrl) {
    // redirected;
} else {
    // not redirected;
}

jamestalmage added a commit to jamestalmage/follow-redirects that referenced this issue Jul 3, 2015
This refactors the code significantly an closes a number
of outstanding issues.

Closes follow-redirects#15 - remove underscore as dependency.
Closes follow-redirects#8  - some options not copied between requests.
Closes follow-redirects#6  - add variable that tells you if it redirected.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants