Rather than build your own using sockets etc I would use httplib Thus would get the data from the http server and parse the headers into a dictionary e.g.

import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()

headers = r1.getheaders()
print(headers)

gives

[('content-length', '16788'), ('accept-ranges', 'bytes'), ('server', 'Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 mod_ssl/2.2.9 OpenSSL/0.9.8g mod_wsgi/2.5 Python/2.5.2'), ('last-modified', 'Mon, 15 Feb 2010 07:30:46 GMT'), ('etag', '"105800d-4194-47f9e9871d580"'), ('date', 'Mon, 15 Feb 2010 21:34:18 GMT'), ('content-type', 'text/html')]

and methods for put to send a dictionary as part of a request.

Answer from mmmmmm on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › convert post request headers to python dictionary
r/learnpython on Reddit: Convert POST request headers to python dictionary
December 10, 2022 -

Let's say I have a POST request that looks something like:

Host: www.domain.com
Cookie: sessions=103232-34324432-524542....34134141 
Content-Length: 55 

... goes on in this format

I would like a script to quickly transform that into the following format:

headers = {}
headers['Host'] = 'www.domain.com'
headers['Cookie'] = 'sessions=103232-34324432-524542....34134141' headers['Content-Length'] = '55' 

... for every line in a file. Or perhaps simply a function capable of interpreting it.

I have found this snippet online:

dict([[h.partition(':')[0], h.partition(':')[2]] for h in rawheaders.split('\n')])

I think it might contains some clues.

Top answer
1 of 7
5

Rather than build your own using sockets etc I would use httplib Thus would get the data from the http server and parse the headers into a dictionary e.g.

import httplib
conn = httplib.HTTPConnection("www.python.org")
conn.request("GET", "/index.html")
r1 = conn.getresponse()

headers = r1.getheaders()
print(headers)

gives

[('content-length', '16788'), ('accept-ranges', 'bytes'), ('server', 'Apache/2.2.9 (Debian) DAV/2 SVN/1.5.1 mod_ssl/2.2.9 OpenSSL/0.9.8g mod_wsgi/2.5 Python/2.5.2'), ('last-modified', 'Mon, 15 Feb 2010 07:30:46 GMT'), ('etag', '"105800d-4194-47f9e9871d580"'), ('date', 'Mon, 15 Feb 2010 21:34:18 GMT'), ('content-type', 'text/html')]

and methods for put to send a dictionary as part of a request.

2 of 7
4

In case you don't find any library solving the problem, here's a naive, untested solution:

def fold(header):
  line = "%s: %s" % (header[0], header[1])
  if len(line) < 998: 
    return line
  else: #fold
    lines = [line]
    while len(lines[-1]) > 998:
      split_this = lines[-1]
      #find last space in longest chunk admissible
      split_here = split_this[:998].rfind(" ")
      del lines[-1]
      lines = lines + [split_this[:split_here]),
                       split_this[split_here:])] #this may still be too long
                                                 #hence the while on lines[-1]
    return "\n".join(lines)

def dict2header(data):
  return "\n".join((fold(header) for header in data.items()))

def header2dict(data):
  data = data.replace("\n ", " ").splitlines()
  headers = {}
  for line in data:
    split_here = line.find(":")
    headers[line[:split_here]] = line[split_here:]
  return headers
Discussions

Get a header with Python and convert in JSON (requests - urllib2 - json) - Stack Overflow
I’m trying to get the header from a website, encode it in JSON to write it to a file. I’ve tried two different ways without success. FIRST with urllib2 and json import urllib2 import json host = (" More on stackoverflow.com
🌐 stackoverflow.com
Sending header requests via dictionary in python - Stack Overflow
I am making a very obvious mistake which I am not able to figure out. Below is the code snippet: def test_chrome_header(): headers = {1:"'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 1... More on stackoverflow.com
🌐 stackoverflow.com
December 5, 2020
How to print out http-response header in Python - Stack Overflow
Today I actually needed to retrieve data from the http-header response. But since I've never done it before and also there is not much you can find on Google about this. I decided to ask my questio... More on stackoverflow.com
🌐 stackoverflow.com
Requests: Pretty print http response headers
I am using the requests library to interact with an API. If I need to pretty print the response body it is pretty straightforward. I just use the json library, no mystery there. But when it comes to the response headers that are returned as a dict. I am having a hard time to pretty print anything ... More on forum.freecodecamp.org
🌐 forum.freecodecamp.org
2
0
October 30, 2022
🌐
GitHub
gist.github.com › scraperdragon › 3424623
Convert Chrome headers to Python's Requests dictionary · GitHub
August 22, 2012 - Convert Chrome headers to Python's Requests dictionary - chrome2requests.py
🌐
Real Python
realpython.com › lessons › responses-content-and-headers
Responses: Content and Headers (Video) – Real Python
json(): You can use the json() method to get the response content in JSON format · The headers attributes returns a Python dictionary with all of the response headers.
Published   March 6, 2019
Top answer
1 of 4
17

There are more than a couple ways to encode headers as JSON, but my first thought would be to convert the headers attribute to an actual dictionary instead of accessing it as requests.structures.CaseInsensitiveDict

import requests, json
r = requests.get("https://www.python.org/")
rh = json.dumps(r.headers.__dict__['_store'])
print rh

{'content-length': ('content-length', '45474'), 'via': ('via', '1.1 varnish'), 'x-cache': ('x-cache', 'HIT'), 'accept-ranges': ('accept-ranges', 'bytes'), 'strict-transport-security': ('strict-transport-security', 'max-age=63072000; includeSubDomains'), 'vary': ('vary', 'Cookie'), 'server': ('server', 'nginx'), 'x-served-by': ('x-served-by', 'cache-iad2132-IAD'), 'x-cache-hits': ('x-cache-hits', '1'), 'date': ('date', 'Wed, 02 Jul 2014 14:13:37 GMT'), 'x-frame-options': ('x-frame-options', 'SAMEORIGIN'), 'content-type': ('content-type', 'text/html; charset=utf-8'), 'age': ('age', '1483')}

Depending on exactly what you want on the headers you can specifically access them after this, but this will give you all the information contained in the headers, if in a slightly different format.

If you prefer a different format, you can also convert your headers to a dictionary:

import requests, json
r = requests.get("https://www.python.org/")
print json.dumps(dict(r.headers))

{"content-length": "45682", "via": "1.1 varnish", "x-cache": "HIT", "accept-ranges": "bytes", "strict-transport-security": "max-age=63072000; includeSubDomains", "vary": "Cookie", "server": "nginx", "x-served-by": "cache-at50-ATL", "x-cache-hits": "5", "date": "Wed, 02 Jul 2014 14:08:15 GMT", "x-frame-options": "SAMEORIGIN", "content-type": "text/html; charset=utf-8", "age": "951"}

2 of 4
10

If you are only interested in the header, make a head request. convert the CaseInsensitiveDict in a dict object and then convert it to json.

import requests
import json
r = requests.head('https://www.python.org/')
rh = dict(r.headers)
json.dumps(rh)
🌐
Stack Overflow
stackoverflow.com › questions › 65152511 › sending-header-requests-via-dictionary-in-python
Sending header requests via dictionary in python - Stack Overflow
December 5, 2020 - def test_chrome_header(): headers = {1:"'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.97'", 2:"'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'"} for key, header in headers.items(): try: response = requests.get("https://www.example.com", proxies=proxies, headers=header, verify=False) response.raise_for_status() print(response.status_code) except HTTPError as http_err: print('HTTP error occurred: {%s}'%http_err) except Exception as err: print(
Top answer
1 of 8
28

Update: Based on comment of OP, that only the response headers are needed. Even more easy as written in below documentation of Requests module:

We can view the server's response headers using a Python dictionary:

Copy>>> r.headers
{
    'content-encoding': 'gzip',
    'transfer-encoding': 'chunked',
    'connection': 'close',
    'server': 'nginx/1.0.4',
    'x-runtime': '148ms',
    'etag': '"e1ca502697e5c9317743dc078f67693f"',
    'content-type': 'application/json'
}

And especially the documentation notes:

The dictionary is special, though: it's made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.

So, we can access the headers using any capitalization we want:

and goes on to explain even more cleverness concerning RFC compliance.

The Requests documentation states:

Using Response.iter_content will handle a lot of what you would otherwise have to handle when using Response.raw directly. When streaming a download, the above is the preferred and recommended way to retrieve the content.

It offers as example:

Copy>>> r = requests.get('https://api.github.com/events', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
>>> r.raw.read(10)
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'

But also offers advice on how to do it in practice by redirecting to a file etc. and using a different method:

Using Response.iter_content will handle a lot of what you would otherwise have to handle when using Response.raw directly

2 of 8
14

Here's how you get just the response headers using the requests library like you mentioned (implementation in Python3):

Copyimport requests

url = "https://www.google.com"
response = requests.head(url)
print(response.headers) # prints the entire header as a dictionary
print(response.headers["Content-Length"]) # prints a specific section of the dictionary

It's important to use .head() instead of .get() otherwise you will retrieve the whole file/page like the rest of the answers mentioned.

If you wish to retrieve a URL that requires authentication you can replace the above response with this:

Copyresponse = requests.head(url, auth=requests.auth.HTTPBasicAuth(username, password))
🌐
Python-requests
docs.python-requests.org › en › latest › user › quickstart
Quickstart — Requests 2.32.3 documentation - docs.python …
We can view the server’s response headers using a Python dictionary: >>> r.headers { 'content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'connection': 'close', 'server': 'nginx/1.0.4', 'x-runtime': '148ms', 'etag': '"e1ca502697e5c9317743dc078f67693f"', 'content-type': 'application/json' } The dictionary is special, though: it’s made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.
Find elsewhere
🌐
freeCodeCamp
forum.freecodecamp.org › python
Requests: Pretty print http response headers - Python - The freeCodeCamp Forum
October 30, 2022 - I am using the requests library to interact with an API. If I need to pretty print the response body it is pretty straightforward. I just use the json library, no mystery there. But when it comes to the response headers that are returned as a dict. I am having a hard time to pretty print anything ...
🌐
Delft Stack
delftstack.com › home › howto › python › requests headers in python
Requests Headers in Python | Delft Stack
March 11, 2025 - This comprehensive guide explores the requests library in Python, focusing on how to implement request headers effectively. Learn to send GET and POST requests with custom headers, handle response headers, and set default headers using sessions. Perfect for developers looking to enhance their ...
🌐
GitHub
gist.github.com › HebeHH › 14382d210c89781c582af6a5a2dadb71
Extracting Headers from cURL request as a dictionary for the python Requests library · GitHub
Extracting Headers from cURL request as a dictionary for the python Requests library - CurlHeaderExtraction.py
🌐
W3Schools
w3schools.com › python › ref_requests_response.asp
Python requests.Response Object
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Training ... The requests.Response() Object contains the server's response to the HTTP request.
🌐
GeeksforGeeks
geeksforgeeks.org › python › response-headers-python-requests
response.headers - Python requests - GeeksforGeeks
July 12, 2025 - The response.headers object in Python's requests library functions as a special dictionary that contains extra information provided by the server when we make an HTTP request. It stores metadata like content type, server details and other headers, ...
🌐
datagy
datagy.io › home › python requests › using headers with python requests
Using Headers with Python requests • datagy
December 30, 2022 - In the two previous sections, you ... be returned in a Response object. The headers are accessible via the .headers attribute, which returns a dictionary....
🌐
Real Python
realpython.com › python-requests
Python's Requests Library (Guide) – Real Python
July 23, 2025 - To add headers to requests, pass a dictionary of headers to the headers parameter in your request. To send POST data, use the data parameter for form-encoded data or the json parameter for JSON data. response.text gives ...
🌐
GitHub
github.com › slackapi › bolt-python › blob › main › slack_bolt › response › response.py
bolt-python/slack_bolt/response/response.py at main · slackapi/bolt-python
headers: Optional[Dict[str, Union[str, Sequence[str]]]] = None, ): """The response from a Bolt app. · Args: status: HTTP status code · body: The response body (dict and str are supported) headers: The response headers.
Author   slackapi
🌐
ReqBin
reqbin.com › code › python › jyhvwlgz › python-requests-headers-example
How to send HTTP headers using Python Requests Library?
The Python Requests Library stores the server's response HTTP headers as a Python dictionary in the response.headers object. You can work with headers object as you would with a regular Python dictionary.
🌐
Scrapy
docs.scrapy.org › en › latest › topics › request-response.html
Requests and Responses — Scrapy 2.16.0 documentation
3 weeks ago - The dict values can be strings (for single valued headers) or lists (for multi-valued headers). body (bytes) – the response body. To access the decoded text as a string, use response.text from an encoding-aware Response subclass, such as TextResponse.
🌐
Requests
requests.readthedocs.io › en › latest › user › quickstart
Quickstart — Requests 2.32.5 documentation - Read the Docs
We can view the server’s response headers using a Python dictionary: >>> r.headers { 'content-encoding': 'gzip', 'transfer-encoding': 'chunked', 'connection': 'close', 'server': 'nginx/1.0.4', 'x-runtime': '148ms', 'etag': '"e1ca502697e5c9317743dc078f67693f"', 'content-type': 'application/json' } The dictionary is special, though: it’s made just for HTTP headers. According to RFC 7230, HTTP Header names are case-insensitive.