rest_patch_resource.erl 920 B

123456789101112131415161718192021222324252627282930313233
  1. -module(rest_patch_resource).
  2. -export([init/3, allowed_methods/2, content_types_provided/2, get_text_plain/2,
  3. content_types_accepted/2, patch_text_plain/2]).
  4. init(_Transport, _Req, _Opts) ->
  5. {upgrade, protocol, cowboy_rest}.
  6. allowed_methods(Req, State) ->
  7. {[<<"HEAD">>, <<"GET">>, <<"PATCH">>], Req, State}.
  8. content_types_provided(Req, State) ->
  9. {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
  10. get_text_plain(Req, State) ->
  11. {<<"This is REST!">>, Req, State}.
  12. content_types_accepted(Req, State) ->
  13. case cowboy_req:method(Req) of
  14. <<"PATCH">> ->
  15. {[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req, State};
  16. _ ->
  17. {[], Req, State}
  18. end.
  19. patch_text_plain(Req, State) ->
  20. case cowboy_req:body(Req) of
  21. {ok, <<"halt">>, Req0} ->
  22. {halt, cowboy_req:reply(400, Req0), State};
  23. {ok, <<"false">>, Req0} ->
  24. {false, Req0, State};
  25. {ok, _Body, Req0} ->
  26. {true, Req0, State}
  27. end.