anilmwr
anilmwr

Reputation: 477

Python requests module - send only specified headers

I need to send a post request to a web server with only 'Host' & 'Content-Length' headers. I have specified these two headers in the dictionary which I pass to requests module, but it adds 'Accept', 'Accept-Encoding', 'User-Agent' headers.

Python code:

headers = {'Content-Length': content_length, 'Host': 'Server-1:8080'}
r = requests.post(url, data=data, headers=headers)
print(r.request.headers)

Actual request headers sent:

{'Accept': '*/*', 'Host': 'Server-1:8080', 'Content-Length': '3072', 'User-Agent': 'python-requests/2.6.0 CPython/3.4.1 Windows/7', 'Connection': 'keep-alive, 'Accept-Encoding': 'gzip, deflate'}

How can I limit the headers sent by requests module?

Upvotes: 5

Views: 3490

Answers (1)

Jamie Bull
Jamie Bull

Reputation: 13519

From a read of the docs it looks like those are session level parameters which you can force to be omitted by setting their values to None.

headers = {'Content-Length': content_length, 'Host': 'Server-1:8080',
           'User-Agent': None, 'Connection': None, 'Accept-Encoding': None}

Upvotes: 6

Related Questions