cowboy_req.read_part.asciidoc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. = cowboy_req:read_part(3)
  2. == Name
  3. cowboy_req:read_part - Read the next multipart headers
  4. == Description
  5. [source,erlang]
  6. ----
  7. read_part(Req :: cowboy_req:req())
  8. -> read_part(Req, #{})
  9. read_part(Req :: cowboy_req:req(), Opts)
  10. -> {ok, Headers, Req} | {done, Req}
  11. Opts :: cowboy_req:read_body_opts()
  12. Headers :: #{binary() => binary()}
  13. ----
  14. Read the next part of a multipart body.
  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 parses and returns
  18. headers. Examples of multipart media types are
  19. `multipart/form-data` and `multipart/byteranges`.
  20. Cowboy will skip any data remaining until the beginning of
  21. the next part. This includes the preamble to the multipart
  22. message but also the body of a previous part if it hasn't
  23. been read. Both are skipped automatically when calling this
  24. function.
  25. Cowboy will read the body before parsing in chunks of size
  26. up to 64KB, with a period of 5 seconds. This is tailored for
  27. reading part headers and might not be the most efficient for
  28. skipping the previous part's body.
  29. The headers returned are MIME headers, *NOT* HTTP headers.
  30. They can be parsed using the functions from the `cow_multipart`
  31. module. In addition, the `cow_multipart:form_data/1` function
  32. can be used to quickly extract information from `multipart/form-data`
  33. representations.
  34. // @todo Proper link to cow_multipart:form_data.
  35. Once a part has been read, it can not be read again.
  36. Once the body has been read, Cowboy sets the content-length
  37. header if it was not previously provided.
  38. // @todo Limit the maximum size of multipart headers.
  39. == Arguments
  40. Req::
  41. The Req object.
  42. Opts::
  43. A map of body reading options. Please refer to
  44. link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)]
  45. for details about each option.
  46. +
  47. This function defaults the `length` to 64KB and the `period`
  48. to 5 seconds.
  49. == Return value
  50. An `ok` tuple is returned containing the next part's headers
  51. as a map.
  52. A `done` tuple is returned if there are no more parts to read.
  53. The Req object returned in the tuple must be used for that point
  54. onward. It contains a more up to date representation of the request.
  55. For example it may have an added content-length header once the
  56. body has been read.
  57. == Changelog
  58. * *2.0*: Function introduced. Replaces `part/1,2`.
  59. == Examples
  60. .Read all parts
  61. [source,erlang]
  62. ----
  63. acc_multipart(Req0, Acc) ->
  64. case cowboy_req:read_part(Req0) of
  65. {ok, Headers, Req1} ->
  66. {ok, Body, Req} = stream_body(Req1, <<>>),
  67. acc_multipart(Req, [{Headers, Body}|Acc]);
  68. {done, Req} ->
  69. {lists:reverse(Acc), Req}
  70. end.
  71. stream_body(Req0, Acc) ->
  72. case cowboy_req:read_part_body(Req0) of
  73. {more, Data, Req} ->
  74. stream_body(Req, << Acc/binary, Data/binary >>);
  75. {ok, Data, Req} ->
  76. {ok, << Acc/binary, Data/binary >>, Req}
  77. end.
  78. ----
  79. .Read all part headers, skipping bodies
  80. [source,erlang]
  81. ----
  82. skip_body_multipart(Req0, Acc) ->
  83. case cowboy_req:read_part(Req0) of
  84. {ok, Headers, Req} ->
  85. skip_body_multipart(Req, [Headers|Acc]);
  86. {done, Req} ->
  87. {lists:reverse(Acc), Req}
  88. end.
  89. ----
  90. .Read a part header in larger chunks
  91. [source,erlang]
  92. ----
  93. {ok, Headers, Req} = cowboy_req:read_part(Req0, #{length => 1000000}).
  94. ----
  95. == See also
  96. link:man:cowboy_req(3)[cowboy_req(3)],
  97. link:man:cowboy_req:has_body(3)[cowboy_req:has_body(3)],
  98. link:man:cowboy_req:body_length(3)[cowboy_req:body_length(3)],
  99. link:man:cowboy_req:read_body(3)[cowboy_req:read_body(3)],
  100. link:man:cowboy_req:read_urlencoded_body(3)[cowboy_req:read_urlencoded_body(3)],
  101. link:man:cowboy_req:read_part_body(3)[cowboy_req:read_part_body(3)]