question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Response history is not available in response hooks during redirections

See original GitHub issue

Response history is not available in response hooks during multiple redirections. For example, if a GET-request gets redirected 5 times, the response history stays empty when examined from inside a response hook after each redirection. However the response-object that get() returns will contain the full history.

In some cases it would be useful to know, for example, what the initial requested URL was from inside a hook. However, currently I think it’s not possible due to the lack of history.

Expected Result

Up-to-date history available in response hook after each redirection.

Hook history for https://ca.fi/r1.php: []
Hook history for https://ca.fi/r2.php: [<Response [302]>]
Hook history for https://ca.fi/r3.php: [<Response [302]>, <Response [302]>]
Hook history for https://ca.fi/r4.php: [<Response [302]>, <Response [302]>, <Response [302]>]
Response history for https://ca.fi/r4.php: [<Response [302]>, <Response [302]>, <Response [302]>]

Actual Result

Response history is empty when examined from response hooks.

Hook history for https://ca.fi/r1.php: []
Hook history for https://ca.fi/r2.php: []
Hook history for https://ca.fi/r3.php: []
Hook history for https://ca.fi/r4.php: []
Response history for https://ca.fi/r4.php: [<Response [302]>, <Response [302]>, <Response [302]>]

Reproduction Steps

import requests

def print_resp_hist(r, *args, **kwargs):
    print("Hook history for %s: %s" % (r.url, r.history))
    return r

r = requests.get(
    'https://ca.fi/r1.php',
    hooks={'response': [print_resp_hist]},
    allow_redirects = True
)

print("Response history for %s: %s" % (r.url, r.history))

System Information

$ python -m requests.help
{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": "3.0"
  },
  "idna": {
    "version": "2.10"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.8.2"
  },
  "platform": {
    "release": "5.4.0-42-generic",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "1010107f",
    "version": "19.1.0"
  },
  "requests": {
    "version": "2.24.0"
  },
  "system_ssl": {
    "version": "1010106f"
  },
  "urllib3": {
    "version": "1.25.10"
  },
  "using_pyopenssl": true
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
sigmavirus24commented, Aug 30, 2020

Your hook needs only be callable.


class Hook:
    def __init__(self):
        self.urls = []

    def __call__(self, resposne):
        self.urls.append(response.request.url)


requests.get(url, hooks={"response": [Hook()]}, ...)

Should do what you want as a basic skeleton

1reaction
sigmavirus24commented, Aug 31, 2020

That looks to be in resolve_redirects so you’re breaking history and not returning accurate history for each part of the redirect cycle. You may find it does near-enough to what you want, but I’d strongly discourage others from using that patch.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Failure to get through digest page which redirects ...
I have to assume it's a bug. If it isn't, there's effectively no way to intercept a redirect and put a hook on...
Read more >
python - requests.history returning empty output on Zapier
So that part is working fine. Per the documentation, response.history only fills the array if any redirects happened.
Read more >
React Router v5 - Fix for redirects not rendering when ...
x, I was using "react-router-dom": "^5.2.0" with "history": "^5.0.0" and my redirects were updating the url but not rendering the component or ...
Read more >
Response.redirect() - Web APIs - MDN Web Docs
The redirect() method of the Response interface returns a Response resulting in a redirect to the specified URL.
Read more >
Using redirects - AWS Amplify Hosting
Redirects enable a web server to reroute navigation from one URL to another. Common reasons for using redirects include: to customize the appearance...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found