I'm using the python requests module to do some testing against a site.
The requests module allows you to remove certain headers by passing in a dictionary with the keys set to None. For example
headers = {u'User-Agent': None}
will ensure that no user agent is sent with the request.
However, it seems that when I post data, requests will calculate the correct Content-Length for me even if I specify None, or an incorrect value. Eg.
headers = {u'Content-Length': u'999'}
headers = {u'Content-Length': None}
I check the response for the headers used in the request (response.request.headers) and I can see the the Content-Length has been re-added with the correct value. So Far I cannot see any way to disable this behaviour
CaseInsensitiveDict({'Content-Length': '39', 'Content-Type': 'application/x-www-form-urlencoded', 'Accept-Encoding': 'gzip, deflate, compress', 'Accept': '*/*', 'User-Agent': 'python-requests/2.2.1 CPython/2.7.6 Linux/3.13.0-36-generic'})
I'd REALLY like to remain with the requests module to do this. Is this possible?
You'll have to prepare the request manually, then remove the generated content-length header:
Do note that most compliant HTTP servers may then ignore your post body!
If you meant to use chunked transfer encoding (where you don't have to send a content length), then use a iterator for the
data
parameter. See Chunked-Encoded Requests in the documentation. NoContent-Length
header will be set in that case.