cowboy_req.read_body.asciidoc 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. = cowboy_req:read_body(3)
  2. == Name
  3. cowboy_req:read_body - Read the request body
  4. == Description
  5. [source,erlang]
  6. ----
  7. read_body(Req :: cowboy_req:req())
  8. -> read_body(Req, #{})
  9. read_body(Req :: cowboy_req:req(), Opts)
  10. -> {ok, Data :: binary(), Req}
  11. | {more, Data :: binary(), Req}
  12. Opts :: cowboy_req:read_body_opts()
  13. ----
  14. Read the request body.
  15. This function reads a chunk of the request body. A `more` tuple
  16. is returned when more data remains to be read. Call the function
  17. repeatedly until an `ok` tuple is returned to read the entire body.
  18. An `ok` tuple with empty data is returned when the request has no body,
  19. or when calling this function again after the body has already
  20. been read. It is therefore safe to call this function directly.
  21. Note that the body can only be read once.
  22. This function reads the request body from the connection process.
  23. The connection process is responsible for reading from the socket.
  24. The exact behavior varies depending on the protocol.
  25. The options therefore are only related to the communication
  26. between the request process and the connection process.
  27. Cowboy will automatically handle protocol details including
  28. the expect header, chunked transfer-encoding and others.
  29. Once the body has been read fully, Cowboy sets the content-length
  30. header if it was not previously provided.
  31. == Arguments
  32. Req::
  33. The Req object.
  34. Opts::
  35. A map of body reading options.
  36. +
  37. The `length` option can be used to request smaller or bigger
  38. chunks of data to be sent. It is a best effort approach, Cowboy
  39. may send more data than configured on occasions. It defaults
  40. to 8MB.
  41. +
  42. The `period` indicates how long the connection process will wait
  43. before it provides us with the data it received. It defaults
  44. to 15 seconds.
  45. +
  46. The connection process sends data to the request process when
  47. either the `length` of data or the `period` of time is reached.
  48. +
  49. The `timeout` option is a safeguard in case the connection
  50. process becomes unresponsive. The function will crash if no
  51. message was received in that interval. The timeout should be
  52. larger than the period. It defaults to the period + 1 second.
  53. == Return value
  54. A `more` tuple is returned when there are more data to be read.
  55. An `ok` tuple is returned when there are no more data to be read,
  56. either because this is the last chunk of data, the body has already
  57. been read, or there was no body to begin with.
  58. The data is always returned as a binary.
  59. The Req object returned in the tuple must be used from that point
  60. onward. It contains a more up to date representation of the request.
  61. For example it may have an added content-length header once the
  62. body has been read.
  63. == Changelog
  64. * *2.0*: Function introduced. Replaces `body/1,2`.
  65. == Examples
  66. .Read the entire body
  67. [source,erlang]
  68. ----
  69. read_body(Req0, Acc) ->
  70. case cowboy_req:read_body(Req0) of
  71. {ok, Data, Req} -> {ok, << Acc/binary, Data/binary >>, Req};
  72. {more, Data, Req} -> read_body(Req, << Acc/binary, Data/binary >>)
  73. end.
  74. ----
  75. .Read the body in small chunks
  76. [source,erlang]
  77. ----
  78. cowboy_req:read_body(Req, #{length => 64000}).
  79. ----
  80. == See also
  81. link:man:cowboy_req(3)[cowboy_req(3)],
  82. link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)],
  83. link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)],
  84. link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)],
  85. link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)],
  86. link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)]