Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -module(test_method).
- %-compile([export_all, nowarn_export_all]).
- % standard callbacks
- -export([init/2, allowed_methods/2, content_types_provided/2, content_types_accepted/2, resource_exists/2]).
- % custom callbacks
- -export([method_jsone/2]).
- -export([create_jsone/2]).
- -export([create_jsone2/4]).
- -export([valid_path/1]).
- init(Req, Opts) ->
- {cowboy_rest, Req, Opts}.
- allowed_methods(Req, State) ->
- {[<<"GET">>, <<"POST">>], Req, State}.
- %{[<<"POST">>], Req, State}.
- content_types_provided(Req, State) ->
- {[
- %{ {<<"text">>, <<"plain">>, []}, method_jsone},
- %{ {<<"text">>, <<"html">>, []}, method_jsone},
- %{ {<<"application">>, <<"json">>, []}, method_jsone}
- { {<<"text">>, <<"plain">>, '*'}, method_jsone},
- { {<<"text">>, <<"html">>, '*'}, method_jsone},
- { {<<"application">>, <<"json">>, '*'}, method_jsone}
- ], Req, State}.
- content_types_accepted(Req, State) ->
- { [{ {<<"application">>, <<"x-www-form-urlencoded">>, []}, create_jsone}], Req, State}.
- resource_exists(Req, State) ->
- io:format("resource_exists State: ~p~n", [State]),
- {ok, Params, Req2} = cowboy_req:read_urlencoded_body(Req),
- io:format("Post params: ~p~n",[Params]),
- case lists:keyfind(<<"test_id">>, 1, Params) of
- false -> {false, Req2, [{test_id, <<>>} | State]};
- {_, Test_Id} -> {?MODULE:valid_path(Test_Id), Req2, [{test_id, Test_Id} | State]}
- end.
- create_jsone(Req, State) ->
- io:format("create0_jsone: ~p~n", ["7799"]),
- {test_id, Some_Id} = lists:keyfind(test_id, 1, State),
- io:format("create_jsone: ~p~n", [Some_Id]),
- ?MODULE:create_jsone2(cowboy_req:method(Req), Some_Id, Req, State).
- % helper
- create_jsone2(<<"POST">>, Some_Id, Req2, State) ->
- Req3 = cowboy_req:set_resp_body(["Hello world! id: ", Some_Id], Req2),
- {true, Req3, State};
- create_jsone2(M, _, Req2, State) ->
- io:format("method: ~p~n", [M]),
- {true, Req2, State}.
- % cowboy_req:binding(test_id, Req, undefined) % get value from router bind,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.binding/
- % #{lang := Lang} = cowboy_req:match_qs([{lang, [], plain}], Req), % get_params,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.match_qs/
- % {ok, [{<<"paste">>, Value}], Req2} = cowboy_req:read_urlencoded_body(Req), % post_params,, https://ninenines.eu/docs/en/cowboy/2.4/manual/cowboy_req.read_urlencoded_body/
- method_jsone(Req, not_found) ->
- { <<"{\"status\":\"error\", \"error_message\":\"invalid data\"}">>, Req, not_found};
- method_jsone(Req, Id) ->
- { <<"{\"status\":\"ok\", \"data\":[{\"id\":\"", Id/binary, "\"}]}">>, Req, Id}.
- % %%%%%%%%%%%%%%%%%%%%%%%55
- valid_path(<<>>) -> true;
- valid_path(<<$., _T/binary>>) -> false;
- valid_path(<<$/, _T/binary>>) -> false;
- valid_path(<<_Char, T/binary>>) -> valid_path(T).
- % Escape some HTML characters that might make a fuss
- escape_html_chars(Bin) ->
- << <<(escape_html_char(B))/binary>> || <<B>> <= Bin >>.
- escape_html_char($<) -> <<"<">>;
- escape_html_char($>) -> <<">">>;
- escape_html_char($&) -> <<"&">>;
- escape_html_char(C) -> <<C>>.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement