cowboy_req.match_cookies.asciidoc 2.0 KB

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