http_multipart.erl 537 B

12345678910111213141516171819202122
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(http_multipart).
  3. -export([init/2]).
  4. -export([handle/2]).
  5. init(Req, Opts) ->
  6. {http, Req, Opts}.
  7. handle(Req, State) ->
  8. {Result, Req2} = acc_multipart(Req, []),
  9. {ok, cowboy_req:reply(200, [], term_to_binary(Result), Req2), State}.
  10. acc_multipart(Req, Acc) ->
  11. case cowboy_req:part(Req) of
  12. {ok, Headers, Req2} ->
  13. {ok, Body, Req3} = cowboy_req:part_body(Req2),
  14. acc_multipart(Req3, [{Headers, Body}|Acc]);
  15. {done, Req2} ->
  16. {lists:reverse(Acc), Req2}
  17. end.