http_multipart.erl 633 B

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