You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am struggling with getting redirect url from a GET request with httpx.
Using requests
url="https://news.google.com/rss/articles/CBMifmh0dHBzOi8vd3d3LnNwaWVnZWwuZGUvd2lydHNjaGFmdC90ZXNsYS1zdHJlaWNodC16ZWhuLXByb3plbnQtYWxsZXItam9icy1pbS11bnRlcm5laG1lbi1hLTYyMTkxYzk1LTE5MTAtNDI4Mi05Y2M2LTk5N2I0OTE1NWM0M9IBAA?oc=5"# Send the request and allow redirectionresponse=requests.get(url, allow_redirects=True)
# Check if the request was redirectedifresponse.history:
last_redirection=response.history[-1]
print("Redirection Status Code:", last_redirection.status_code)
print("Redirection URL:", last_redirection.headers["location"])
else:
print("No redirection occurred.")
Output:
Redirection Status Code: 302
Redirection Headers: https://www.spiegel.de/wirtschaft/tesla-streicht-zehn-prozent-aller-jobs-im-unternehmen-a-62191c95-1910-4282-9cc6-997b49155c43
I can get the redirect url using requests.
Using httpx
url="https://news.google.com/rss/articles/CBMifmh0dHBzOi8vd3d3LnNwaWVnZWwuZGUvd2lydHNjaGFmdC90ZXNsYS1zdHJlaWNodC16ZWhuLXByb3plbnQtYWxsZXItam9icy1pbS11bnRlcm5laG1lbi1hLTYyMTkxYzk1LTE5MTAtNDI4Mi05Y2M2LTk5N2I0OTE1NWM0M9IBAA?oc=5"client=httpx.Client(follow_redirects=True)
# Send the request and capture the responseresponse=client.get(url)
# Check if the request was redirectedifresponse.history:
last_redirection=response.history[-1]
print("Redirection Status Code:", last_redirection.status_code)
print("Redirection URL:", last_redirection.headers["location"])
else:
print("No redirection occurred.")
# Close the clientclient.close()
Output:
Redirection Status Code: 302
Redirection URL: https://news.google.com/rss/articles/CBMifmh0dHBzOi8vd3d3LnNwaWVnZWwuZGUvd2lydHNjaGFmdC90ZXNsYS1zdHJlaWNodC16ZWhuLXByb3plbnQtYWxsZXItam9icy1pbS11bnRlcm5laG1lbi1hLTYyMTkxYzk1LTE5MTAtNDI4Mi05Y2M2LTk5N2I0OTE1NWM0M9IBAA?oc=5&hl=en-US&gl=US&ceid=US:en
I cannot get the Redirect URL when using httpx.
Is there anything wrong with my code? Or it is a bug?
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I am struggling with getting redirect url from a GET request with httpx.
Using requests
Output:
I can get the redirect url using requests.
Using httpx
Output:
I cannot get the Redirect URL when using httpx.
Is there anything wrong with my code? Or it is a bug?
Beta Was this translation helpful? Give feedback.
All reactions