Makar Nikitin
Makar Nikitin

Reputation: 329

Requests adding own header attributes

I am trying to make GET request with empty headers like this:

response = requests.get(url="https://www.google.com", headers={})

However requests adding default headers:

for key, value in response.request.headers.items():
    print(key + ": " + value)
User-Agent: python-requests/2.22.0
Accept-Encoding: gzip, deflate
Accept: */*
Connection: keep-alive

How to override it, and make dict truly empty?

Upvotes: 0

Views: 150

Answers (1)

Xosrov
Xosrov

Reputation: 729

The headers you pass are actually appended to existing defaults set by the library. You'll have to overwrite the dict directly if you want to change the headers' order or remove existing headers; for example by using a session:

import requests
with requests.Session() as sess:
    sess.headers = {}
    response = sess.get(url="https://www.google.com")
    for key, value in response.request.headers.items():
        print(key + ": " + value)

For more about why this happens, check out this post i made on the subject.

Upvotes: 1

Related Questions