HTTPStatus library for Elixir?


I am working on a project where I have to deal with some HTTP status codes (300, 400, 500, etc).
It is not a project where we use Phoenix and such is even out of the question ( the project is not a good fit for the framework ).


In my day to day I find my self having to remember what all those codes mean. Eventually I either forget or make a type, send a 403 instead of a 404, for example.

What am I looking for?

Node has a nice little package called HTTPStatus that helps me with this. With it I can type NOT_FOUND instead of 404 and so on.

I was wondering if Hex has something similar, since I didn’t really find anything.

See Plug.Conn.Status.code/1

1 Like

This only applies for Plug, though.

I get the value proposition. The good thing with a library containing HTTP status codes (as functions returning constants?) is IDE autocompletion for example. But the downside is needing to coordinate with HTTP libraries that they understand these constants. They could be straight integers (def not_found(), do: 404) but some libraries prefer atoms instead, so you’d need to ensure they all agree on the atom naming.

So, I can’t use this in my projects ?
What a disappointment …

Is there any library that I can actually use in my projects?

I believe you can, I don’t see why not. I think @Nicd meant that it’s not a small library that contains just these codes, and you probably don’t want to add plug to your project just for that function if you’re not already using plug.

Also, the usage would look like:

import Plug.Conn.Status, only: [code: 1]
the_code = code(:not_found)

That said, if you’re already using plug and this API suits you, feel free to use it. Otherwise you can copy the code to your own module or hand-roll something similar and publish your library on hex :wink:


You can use :httpd_util.reason_phrase/1. it’s part of erlang


But it doesn’t do what @Fl4m3Ph03n1x wanted? It takes the code and returns the reason, not the other way around :wink:

I did have a library called http_status. In the end I moved it into Raxx because it wasn’t adding enough on it’s own. There are quite a few discussions around non standard responses such as 422 that people often need to tweak the library to their own usecase

It’s easy to write your own function though.
These 24 lines read a file of definitions (copied from RFC7231) checks if any extra codes are set in the config and define a function status_code/1.