3

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'] = ...
5
  • "for obvious reasons." I'm afraid they are not at all obvious at least to me. Care to elaborate? Is this in order to avoid repetition? Commented Aug 12, 2017 at 19:12
  • 2
    Manually typing Header names is prone to typos. Commented Aug 12, 2017 at 19:13
  • Okay, I see. Flask is largely built on top of Werkzeug. I'd check its HTTP utilities. But to be honest, I doubt you'll find anything. Commented Aug 12, 2017 at 19:20
  • I thought I'd find it in http, since it already has the HTTPStatus list - but nope. It's weird. Commented Aug 12, 2017 at 19:26
  • I guess there is little need for it. I also just realised, you may find something in urllib. The chances are slim again, though. Commented Aug 12, 2017 at 19:29

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

Looks good. Ever thought about contributing this to the stdlib? http seems like a good fit, since it already has a similar HTTPStatus class.
Actually, I created this repo, just for you minutes ago :)
Oh... well, do feel encouraged to share it upstream, as well! Thank you :)
Uh ... But that contains all headers. Including those reserved for mail and netnews. Please considder the filtering advices from my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.