rest_param_all.erl 886 B

1234567891011121314151617181920212223242526272829303132333435
  1. -module(rest_param_all).
  2. -export([init/2]).
  3. -export([allowed_methods/2]).
  4. -export([content_types_provided/2]).
  5. -export([get_text_plain/2]).
  6. -export([content_types_accepted/2]).
  7. -export([put_text_plain/2]).
  8. init(Req, Opts) ->
  9. {cowboy_rest, Req, Opts}.
  10. allowed_methods(Req, State) ->
  11. {[<<"GET">>, <<"PUT">>], Req, State}.
  12. content_types_provided(Req, State) ->
  13. {[{{<<"text">>, <<"plain">>, '*'}, get_text_plain}], Req, State}.
  14. get_text_plain(Req, State) ->
  15. {_, _, Param} = cowboy_req:meta(media_type, Req, {{<<"text">>, <<"plain">>}, []}),
  16. Body = if
  17. Param == '*' ->
  18. <<"'*'">>;
  19. Param == [] ->
  20. <<"[]">>;
  21. Param /= [] ->
  22. iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param])
  23. end,
  24. {Body, Req, State}.
  25. content_types_accepted(Req, State) ->
  26. {[{{<<"text">>, <<"plain">>, '*'}, put_text_plain}], Req, State}.
  27. put_text_plain(Req, State) ->
  28. {true, Req, State}.