http_multipart.erl 613 B

123456789101112131415161718192021222324
  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, cowboy_req:reply(200, [], term_to_binary(Result), Req2), State}.
  10. terminate(_, _, _) ->
  11. ok.
  12. acc_multipart(Req, Acc) ->
  13. case cowboy_req:part(Req) of
  14. {ok, Headers, Req2} ->
  15. {ok, Body, Req3} = cowboy_req:part_body(Req2),
  16. acc_multipart(Req3, [{Headers, Body}|Acc]);
  17. {done, Req2} ->
  18. {lists:reverse(Acc), Req2}
  19. end.