cowboy_req.read_part_body.asciidoc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. = cowboy_req:read_part_body(3)
  2. == Name
  3. cowboy_req:read_part_body - Read the current part's body
  4. == Description
  5. [source,erlang]
  6. ----
  7. read_part_body(Req :: cowboy_req:req())
  8. -> read_part_body(Req, #{})
  9. read_part_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 body of the current part of the multipart message.
  15. This function reads the request body and parses it as
  16. multipart. Each parts of a multipart representation have
  17. their own headers and body. This function returns the
  18. body of the current part. Examples of multipart media types
  19. are `multipart/form-data` and `multipart/byteranges`.
  20. This function reads a chunk of the part's body. A `more` tuple
  21. is returned when more data remains to be read. Call the function
  22. repeatedly until an `ok` tuple is returned to read the entire body.
  23. Once a part has been read, it can not be read again.
  24. Once the body has been read, Cowboy sets the content-length
  25. header if it was not previously provided.
  26. // @todo Limit the maximum size of multipart headers.
  27. == Arguments
  28. Req::
  29. The Req object.
  30. Opts::
  31. A map of body reading options. Please refer to
  32. link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)]
  33. for details about each option.
  34. +
  35. This function uses the same default options as the
  36. link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)]
  37. function.
  38. == Return value
  39. A `more` tuple is returned when there are more data to be read.
  40. An `ok` tuple is returned when there are no more data to be read.
  41. The data is always returned as a binary.
  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 `part_body/1,2`.
  48. == Examples
  49. .Read a full part's body
  50. [source,erlang]
  51. ----
  52. stream_body(Req0, Acc) ->
  53. case cowboy_req:read_part_body(Req0) of
  54. {more, Data, Req} ->
  55. stream_body(Req, << Acc/binary, Data/binary >>);
  56. {ok, Data, Req} ->
  57. {ok, << Acc/binary, Data/binary >>, Req}
  58. end.
  59. ----
  60. .Ensure a part's body is smaller than 64KB
  61. [source,erlang]
  62. ----
  63. {ok, Body, Req} = cowboy_req:read_part_body(Req0, #{length => 64000}).
  64. ----
  65. == See also
  66. link:man:cowboy_req(3)[cowboy_req(3)],
  67. link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)],
  68. link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)],
  69. link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)],
  70. link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)],
  71. link:man:cowboy_req:read_part(3)[cowboy_req:read_part(3)]