http_echo_body.erl 560 B

123456789101112131415161718192021
  1. %% Feel free to use, reuse and abuse the code in this file.
  2. -module(http_echo_body).
  3. -export([init/2]).
  4. init(Req, Opts) ->
  5. true = cowboy_req:has_body(Req),
  6. Req3 = case cowboy_req:read_body(Req, [{length, 1000000}]) of
  7. {ok, Body, Req2} -> handle_body(Req2, Body);
  8. {more, _, Req2} -> handle_badlength(Req2)
  9. end,
  10. {ok, Req3, Opts}.
  11. handle_badlength(Req) ->
  12. cowboy_req:reply(413, #{}, <<"Request entity too large">>, Req).
  13. handle_body(Req, Body) ->
  14. Size = cowboy_req:body_length(Req),
  15. Size = byte_size(Body),
  16. cowboy_req:reply(200, #{}, Body, Req).