Skip to content

Instantly share code, notes, and snippets.

@danifus
Last active July 22, 2021 01:19
Show Gist options
  • Save danifus/6df749c10d247b491fbd800e31dc5656 to your computer and use it in GitHub Desktop.
Save danifus/6df749c10d247b491fbd800e31dc5656 to your computer and use it in GitHub Desktop.
Content-Length checking for requests 2
"""Raise an error if the 'Content-Length' header does not match sent bytes.
Requests 2 doesn't raise an error if the value in the 'Content-Length' header
doesn't match the number of bytes actually received. This has been addressed in
requests 3 but the following is handy in the meantime.
https://github.com/psf/requests/pull/3563
https://github.com/psf/requests/issues/4956
"""
from requests.exceptions import HTTPError
class IncompleteRead(HTTPError):
"""Response size did not match the 'Content-Length' header."""
def raise_on_content_length_mismatch(response):
"""Raise an error if the 'Content-Length' header does not match sent bytes.
This function will not raise an error if the 'Content-Length' header is not
present.
"""
content_length = response.headers.get("content-length")
if content_length is None:
return
if int(content_length) != response.raw._fp_bytes_read:
raise IncompleteRead(
"'Content-Length' ({}) did not match actual bytes received ({}).".format(
content_length, response.raw._fp_bytes_read
),
response=response,
)
def strict_raise_for_error(response):
response.raise_for_status()
raise_on_content_length_mismatch(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment