joeButler joeButler - 1 year ago 250
Python Question

Python requests remove the Content-Length header from POST

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?

Answer Source

You'll have to prepare the request manually, then remove the generated content-length header:

from requests import Request, Session

s = Session()
req = Request('POST', url, data=data)
prepped = req.prepare()
del prepped.headers['content-length']
response = s.send(prepped)

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. No Content-Length header will be set in that case.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download