TL; DR
content_types_provided
content_types_accepted
는 동일하지 않다; POST
을 처리 중이므로 나중에 필요합니다. 내가 사용 content_types_provided
콜백 자원을 우선 순위에 따라 제공 매체 유형의 목록을 반환 무엇 Cowboy2.0.0
에서
. 그래서, 때 당신은 사용 : 당신은 기본적으로 지금부터,이 핸들러가 JSON 응답을 지원, 카우보이를 말하고있다
content_types_provided(Req, State) ->
{[
{{<<"application">>, <<"json">>, []}, get_json}
], Req, State}.
합니다. 그렇기 때문에 GET
을 실행하면 HTTP 200 (OK)
...을 성공적으로 얻을 수 있지만 POST
은 작동하지 않습니다.
반면에 content_types_accepted
콜백은 허용 할 내용을 content-types
으로 허용합니다. 당신은 실제로 당신이 allowed_methods
콜백 <<"POST">>
을 추가하기 때문에 POST
요청을 보낼 수 있지만 당신이 application/json
을 수용 할 cowboy_rest
을 말하지 않았다 때문에 HTTP 415 (Unsupported Media Type)
응답됩니다.
이 당신을 위해 작동해야 무언가이다 :
-module(example_handler).
-export([init/2]).
-export([
allowed_methods/2,
content_types_accepted/2,
content_types_provided/2
]).
-export([get_json/2, post_json/2]).
%%%==============================================
%%% Exports
%%%==============================================
init(Req, Opts) ->
{cowboy_rest, Req, Opts}.
allowed_methods(Req, State) ->
lager:debug("allowed_methods"),
{[<<"GET">>, <<"POST">>], Req, State}.
content_types_accepted(Req, State) ->
lager:debug("content_types_accepted"),
{[
{{<<"application">>, <<"json">>, []}, post_json}
], Req, State}.
content_types_provided(Req, State) ->
lager:debug("content_types_provided"),
{[
{{<<"application">>, <<"json">>, []}, get_json}
], Req, State}.
get_json(Req, State) ->
lager:debug("get_json"),
{<<"{ \"hello\": \"there\" }">>, Req, State}.
post_json(Req, State) ->
lager:debug("post_json"),
{true, Req, State}.
%%%==============================================
%%% Internal
%%%==============================================
당신이 당신의 자신에 일을 시작하고, 당신이 뭔가에 집착 할 때, 특히 그것에 대해 물어 본다면 그것은 좋은 것입니다. –
@ ɐuıɥɔɐɯ 나는 내 질문을 변경했다. 대답 해 주시겠습니까? – Hemul