match_qs(Fields :: cowboy:fields(), Req :: cowboy_req:req()) -> #{atom() => any()}
cowboy_req:match_qs - Match the query string against constraints
match_qs(Fields :: cowboy:fields(), Req :: cowboy_req:req()) -> #{atom() => any()}
Parse the query string and match specific values against constraints.
Cowboy will only return the query string values specified in the fields list, and ignore all others. Fields can be either the key requested; the key along with a list of constraints; or the key, a list of constraints and a default value in case the key is missing.
This function will crash if the key is missing and no default value is provided. This function will also crash if a constraint fails.
The key must be provided as an atom. The key of the returned map will be that atom. The value may be converted through the use of constraints, making this function able to extract, validate and convert values all in one step.
Fields to retrieve from the query string.
See cowboy(3) for a complete description.
The Req object.
Desired values are returned as a map. The key is the atom that was given in the list of fields, and the value is the optionally converted value after applying constraints.
The map contains the same keys that were given in the fields.
An exception is triggered when the match fails.
%% ID and Lang are binaries. #{id := ID, lang := Lang} = cowboy_req:match_qs([id, lang], Req).
%% ID is an integer and Lang a non-empty binary. #{id := ID, lang := Lang} = cowboy_req:match_qs([{id, int}, {lang, nonempty}], Req).
#{lang := Lang} = cowboy_req:match_qs([{lang, [], <<"en-US">>}], Req).
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.