cowboy_req.read_urlencoded_body.asciidoc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. = cowboy_req:read_urlencoded_body(3)
  2. == Name
  3. cowboy_req:read_urlencoded_body - Read and parse a urlencoded request body
  4. == Description
  5. [source,erlang]
  6. ----
  7. read_urlencoded_body(Req :: cowboy_req:req())
  8. -> read_urlencoded_body(Req, #{})
  9. read_urlencoded_body(Req :: cowboy_req:req(), Opts)
  10. -> {ok, Body, Req}
  11. Opts :: cowboy_req:read_body_opts()
  12. Body :: [{Key :: binary(), Value :: binary() | true}]
  13. ----
  14. Read and parse a urlencoded request body.
  15. This function reads the request body and parses it as
  16. `application/x-www-form-urlencoded`. It returns a list
  17. of key/values.
  18. The urlencoded media type is used by Web browsers when
  19. submitting HTML forms using the POST method.
  20. Cowboy needs to read the full body before parsing. By default
  21. it will read bodies of size up to 64KB. It is possible to
  22. provide options to read larger bodies if required.
  23. Cowboy will automatically handle protocol details including
  24. the expect header, chunked transfer-encoding and others.
  25. Once the body has been read, Cowboy sets the content-length
  26. header if it was not previously provided.
  27. This function can only be called once. Calling it again will
  28. result in undefined behavior.
  29. == Arguments
  30. Req::
  31. The Req object.
  32. Opts::
  33. A map of body reading options. Please refer to
  34. link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)]
  35. for details about each option.
  36. +
  37. This function defaults the `length` to 64KB and the `period`
  38. to 5 seconds.
  39. == Return value
  40. An `ok` tuple is returned containing a list of key/values found
  41. in the body.
  42. The Req object returned in the tuple must be used for that point
  43. onward. It contains a more up to date representation of the request.
  44. For example it may have an added content-length header once the
  45. body has been read.
  46. == Changelog
  47. * *2.0*: Function introduced. Replaces `body_qs/1,2`.
  48. == Examples
  49. .Read a urlencoded body
  50. [source,erlang]
  51. ----
  52. {ok, Body, Req} = cowboy_req:read_urlencoded_body(Req0),
  53. {_, Lang} = lists:keyfind(<<"lang">>, 1, Body).
  54. ----
  55. .Allow large urlencoded bodies
  56. [source,erlang]
  57. ----
  58. {ok, Body, Req} = cowboy_req:read_urlencoded_body(Req0, #{length => 1000000}).
  59. ----
  60. == See also
  61. link:man:cowboy_req(3)[cowboy_req(3)],
  62. link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)],
  63. link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)],
  64. link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)],
  65. link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)],
  66. link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)]