Skip to content

Instantly share code, notes, and snippets.

@sakurai-youhei
Created June 16, 2021 12:35
Show Gist options
  • Save sakurai-youhei/9e8148877c52f9007b28021ba4fbc819 to your computer and use it in GitHub Desktop.
Save sakurai-youhei/9e8148877c52f9007b28021ba4fbc819 to your computer and use it in GitHub Desktop.
Sample code for "multiple message-header fields with the same field-name" in Python plus requests
from collections import OrderedDict
from http.client import HTTPMessage
class MultiValueHeaders(HTTPMessage):
'''https://www.w3.org/Protocols/rfc2616/rfc2616-sec4.html#sec4.2'''
_separator_ = ', '
def items(self):
headers = OrderedDict()
for key, value in super().items():
headers.setdefault(key, []).append(value)
return [(key, self._separator_.join(headers[key])) for key in headers]
import requests
headers = MultiValueHeaders()
headers.add_header('x-test', 'v1')
headers.add_header('x-test', 'v2')
with requests.get('http://localhost:8000/', headers=headers):
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment