rest_patch_resource.erl 958 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. -module(rest_patch_resource).
  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([patch_text_plain/2]).
  8. init(Req, Opts) ->
  9. {cowboy_rest, Req, Opts}.
  10. allowed_methods(Req, State) ->
  11. {[<<"HEAD">>, <<"GET">>, <<"PATCH">>], Req, State}.
  12. content_types_provided(Req, State) ->
  13. {[{{<<"text">>, <<"plain">>, []}, get_text_plain}], Req, State}.
  14. get_text_plain(Req, State) ->
  15. {<<"This is REST!">>, Req, State}.
  16. content_types_accepted(Req, State) ->
  17. case cowboy_req:method(Req) of
  18. <<"PATCH">> ->
  19. {[{{<<"text">>, <<"plain">>, []}, patch_text_plain}], Req, State};
  20. _ ->
  21. {[], Req, State}
  22. end.
  23. patch_text_plain(Req, State) ->
  24. case cowboy_req:read_body(Req) of
  25. {ok, <<"stop">>, Req0} ->
  26. {stop, cowboy_req:reply(400, Req0), State};
  27. {ok, <<"false">>, Req0} ->
  28. {false, Req0, State};
  29. {ok, _Body, Req0} ->
  30. {true, Req0, State}
  31. end.