cowboy_req:header(3)

Name

cowboy_req:header - HTTP header

Description

header(Name, Req)          -> header(Name, Req, undefined)
header(Name, Req, Default) -> binary() | Default

Name    :: binary()          %% lowercase; case insensitive
Req     :: cowboy_req:req()
Default :: any()

Return the value for the given HTTP header.

The header name must be given as a lowercase binary string. While header names are case insensitive, Cowboy requires them to be given as lowercase to function properly.

Headers can also be obtained using pattern matching:

#{headers := #{Name := Value}} = Req.

Note that this snippet will crash if the header is missing.

Arguments

Name

Desired HTTP header name as a lowercase binary string.

Req

The Req object.

Default

Default value returned when the header is missing.

Return value

The header value is returned as a binary string. When the header is missing, the default argument is returned.

Changelog

  • 2.0: Only the header value is returned, it is no longer wrapped in a tuple.
  • 1.0: Function introduced.

Examples

Get the accept header
Accept = cowboy_req:header(<<"accept">>, Req).
Get the content-length header with a default value
Length = cowboy_req:header(<<"content-length">>, Req, <<"0">>).

See also

cowboy_req(3), cowboy_req:headers(3), cowboy_req:parse_header(3)

Cowboy 2.4 Function Reference

Navigation

Version select

Like my work? Donate!

Donate to Loïc Hoguin because his work on Cowboy, Ranch, Gun and Erlang.mk is fantastic:

Recurring payment options are also available via GitHub Sponsors. These funds are used to cover the recurring expenses like food, dedicated servers or domain names.