0

I want to send the same header parameter twice with python requests. is it possible?

When I try to do the following request, the Requests lib from python ignores one of the passed headers even sending it with different cases:

Example:

import requests

url = "http://www.example.com"

headers = {"test":"test1", 
           "Test":"test2"}

req = requests.get(url, headers=headers)

print(req.request.headers)

2 Answers2

1

HTTP header names are not case-sensitive, so requests handles this correctly. That the keys in the dictionary are case-insensitive is also sometimes mentioned in requests' docs, like Session.headers, Response.headers

ForceBru
  • 41,233
  • 10
  • 61
  • 89
1

requests stores the request headers in a dict, which means every header can only appear once (+ not case-sensitive). So without making changes to the requests library itself it won't be possible to send multiple headers with the same name.

Mi.
  • 509
  • 3
  • 20
  • 1
    A dict is case-sensitive, so your ‘answer’, that this is accounted for by the behaviour of dict, is wrong and isn’t sufficient to explain the behaviour `requests` which is based on the requirements of the HTTP protocol that headers are case-insensitive. – DisappointedByUnaccountableMod Dec 20 '20 at 23:13