raise_for_status()
noun
say it: “RAYZ for STAT-us” /ˈreɪz fɔr ˈstætəs/
A method on a requests response that raises an error when the server answered with a failure code, from 400 to 599.
What does raise_for_status() do?
raise_for_status() is a method on a requests response that raises an HTTPError when the status code is from
400 to 599, and does nothing otherwise, so a failed request can't pass as a good one.
response = requests.get(url, headers=headers, timeout=10)
response.raise_for_status() # HTTPError: 404 Client Error: Not Found for url: ...
forecast = response.json() # reached only when the request worked
In a sentence
- “Put raise_for_status() before json(), so an error page is never read as data.”
- “Calling raise_for_status() turned the 404 into an error the script could catch.”
Level: Journeyman · More Python terms at this level
Updated