cowboy_req.match_qs.asciidoc 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. = cowboy_req:match_qs(3)
  2. == Name
  3. cowboy_req:match_qs - Match the query string against constraints
  4. == Description
  5. [source,erlang]
  6. ----
  7. match_qs(Fields :: cowboy:fields(), Req :: cowboy_req:req())
  8. -> #{atom() => any()}
  9. ----
  10. Parse the query string and match specific values against
  11. constraints.
  12. Cowboy will only return the query string values specified
  13. in the fields list, and ignore all others. Fields can be
  14. either the key requested; the key along with a list of
  15. constraints; or the key, a list of constraints and a
  16. default value in case the key is missing.
  17. This function will crash if the key is missing and no
  18. default value is provided. This function will also crash
  19. if a constraint fails.
  20. The key must be provided as an atom. The key of the
  21. returned map will be that atom. The value may be converted
  22. through the use of constraints, making this function able
  23. to extract, validate and convert values all in one step.
  24. == Arguments
  25. Fields::
  26. Fields to retrieve from the query string.
  27. +
  28. See link:man:cowboy(3)[cowboy(3)] for a complete description.
  29. Req::
  30. The Req object.
  31. == Return value
  32. Desired values are returned as a map. The key is the atom
  33. that was given in the list of fields, and the value is the
  34. optionally converted value after applying constraints.
  35. The map contains the same keys that were given in the fields.
  36. An exception is triggered when the match fails.
  37. == Changelog
  38. * *2.0*: Function introduced.
  39. == Examples
  40. .Match fields
  41. [source,erlang]
  42. ----
  43. %% ID and Lang are binaries.
  44. #{id := ID, lang := Lang}
  45. = cowboy_req:match_qs([id, lang], Req).
  46. ----
  47. .Match fields and apply constraints
  48. [source,erlang]
  49. ----
  50. %% ID is an integer and Lang a non-empty binary.
  51. #{id := ID, lang := Lang}
  52. = cowboy_req:match_qs([{id, int}, {lang, nonempty}], Req).
  53. ----
  54. .Match fields with default values
  55. [source,erlang]
  56. ----
  57. #{lang := Lang}
  58. = cowboy_req:match_qs([{lang, [], <<"en-US">>}], Req).
  59. ----
  60. == See also
  61. link:man:cowboy_req(3)[cowboy_req(3)],
  62. link:man:cowboy_req:qs(3)[cowboy_req:qs(3)],
  63. link:man:cowboy_req:parse_qs(3)[cowboy_req:parse_qs(3)]