irfanka
irfanka

Reputation: 139

How to get a list of standard HTTP header names in Python

I'm writing a Flask app, and I want to avoid manually writing HTTP response header names, for obvious reasons.

Is there a list of standard HTTP header names in the stdlib (or in Flask), that I could import - so that I avoid writing stuff like this:

response.headers['Content-Type'] = ...

Upvotes: 3

Views: 2066

Answers (2)

naren
naren

Reputation: 15233

Here is the list of headers you can use in your python code

>>> import headers
>>> headers.HEADERS.ACCEPT
'Accept'
>>> headers.HEADERS.CONTENT_TYPE
'Content-Type'

Git repo is here https://github.com/Narengowda/http_headers/blob/master/headers.py

Upvotes: 1

DaSourcerer
DaSourcerer

Reputation: 6606

After giving it some thought, I am afraid there will be little in Flask, Werkzeug or the stdlib. So here is a solution: Create your own enum containing the desired header names. You can create that through the IANA MEssage Header Registry. It comes as CSV, so you can generate the enum programmatically. Just see to it that field 3 (1-based) contains http and field 4 is standard.

Upvotes: 2

Related Questions