Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.2k views
in Technique[技术] by (71.8m points)

http - 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 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...