Response history is not available in response hooks during redirections
See original GitHub issueResponse 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:
- Created 3 years ago
- Comments:6 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Your hook needs only be callable.
Should do what you want as a basic skeleton
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.