Created
January 17, 2019 09:40
-
-
Save fenollp/4ba33ef0fdd150440bc43e48292e0a66 to your computer and use it in GitHub Desktop.
Phoenix: capitalize header keys
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
defmodule CapitalizeHeaderKeys do | |
def hook(status, headers, body, req) do | |
headers = capitalize_resp_headers(headers) | |
{:ok, req} = :cowboy_req.reply(status, headers, body, req) | |
req | |
end | |
# Cowboy v1 | |
defp capitalize_resp_headers(headers) when is_list(headers) do | |
:lists.keymap(&:cowboy_bstr.capitalize_token/1, 1, headers) | |
end | |
# Cowboy v2 | |
defp capitalize_resp_headers(headers) when is_map(headers) do | |
Enum.map(headers, fn {k, v} -> {:cowboy_bstr.capitalize_token(k), v} end) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mix.Config | |
config :my_app, MyApp.Endpoint, | |
debug_errors: false, | |
http: [ | |
protocol_options: [ | |
onresponse: {CapitalizeHeaderKeys, :hook} | |
] | |
] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment