rest_param_all.erl 913 B

123456789101112131415161718192021222324252627282930313233343536
  1. -module(rest_param_all).
  2. -export([init/3]).
  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(_Transport, _Req, _Opts) ->
  9. {upgrade, protocol, cowboy_rest}.
  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}, Req2} =
  16. cowboy_req:meta(media_type, Req, {{<<"text">>, <<"plain">>}, []}),
  17. Body = if
  18. Param == '*' ->
  19. <<"'*'">>;
  20. Param == [] ->
  21. <<"[]">>;
  22. Param /= [] ->
  23. iolist_to_binary([[Key, $=, Value] || {Key, Value} <- Param])
  24. end,
  25. {Body, Req2, State}.
  26. content_types_accepted(Req, State) ->
  27. {[{{<<"text">>, <<"plain">>, '*'}, put_text_plain}], Req, State}.
  28. put_text_plain(Req, State) ->
  29. {true, Req, State}.