erlang 如何将正文设置为Cowboy POST响应

8fsztsew  于 2022-12-08  发布在  Erlang
关注(0)|答案(1)|浏览(197)

我有简单的牛仔休息处理程序:

-module(request_handler).
-export([
  init/2,
  allowed_methods/2,
  content_types_accepted/2,
  content_types_provided/2
]).

-export([
  json_request/2,
  json_response/2
]).

init(Req, Opts) ->
  {cowboy_rest, Req, Opts}.

allowed_methods(Req, State) ->
  {[<<"GET">>, <<"POST">>, <<"PATCH">>, <<"DELETE">>], Req, State}.

content_types_accepted(Req, State) ->
  {[
    {<<"application/json">>, json_request}
  ], Req, State}.

content_types_provided(Req, State) ->
  {[
    {<<"application/json">>, json_response}
  ], Req, State}.

json_request(Req, State) ->
  Resp = cowboy_req:set_resp_body(<<"{\"a\":\"b\"}">>, Req),
  cowboy_req:reply(201, Resp),
  {true, Resp, State}.

json_response(Req, State) ->
  {true, Req, State}.

但是,当我发送http请求时,在log中出现一个见不到的错误:
2020-09-17T19:35:58.305000+03:00 error: <0.231.0> [proc_lib:crash_report/4-525] crasher: initial call: cowboy_clear:connection_process/4, pid: <0.231.0>, registered_name: [], error: {function_clause,[cowboy_http,commands,[state,<0.189.0>,http,#Port<0.10>,ranch_tcp,undefined,#{connection_type => supervisor,env => #{dispatch => [{'_',[],[{[<<"api">>,<<"v.0.1">>,'...'],[],request_handler,#{}},{[<<"static">>,'...'],[],cowboy_static,{priv_dir,gateway,"./www/static",[{mimetypes,cow_mimetypes,all}]}},{'_',[],cowboy_static,{priv_file,gateway,"www/index.html"}}]}]},idle_timeout => infinity,inactivity_timeout => infinity},<<>>,#{},{{127,0,0,1},18780},{{127,0,0,1},8585},undefined,undefined,true,2,{ps_request_line,0},infinity,1,done,1000,[{stream,1,{cowboy_stream_h,{state,undefined,http,<0.232.0>,undefined,undefined,undefined,undefined,0,fin,<<"{\r\n \"Name\" : \"Test 2\"}\r\n">>,165,undefined,normal}},<<"POST">>,'HTTP/1.1',undefined,undefined,0,[]}],[{child,<0.232.0>,1,5000,undefined}]},1,[{response,200,#{<<"content-length">> => <<"9">>,<<"content-type">> => [<<"application">>,<<"/">>,<<"json">>,<<>>],<<"date">> => <<"Thu, 17 Sep 2020 16:35:57 GMT">>,<<"server">> => <<"Cowboy">>},<<"{\"a\":\"b\"}">>}]],[{file,"d:/gateway/_build/default/lib/cowboy/src/cowboy_http.erl"},{line,954}]},{cowboy_http,loop,1,[{file,"d:/gateway/_build/default/lib/cowboy/src/cowboy_http.erl"},{line,254}]},{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,226}]}]}, ancestors: [<0.189.0>,<0.188.0>,ranch_sup,<0.113.0>], message_queue_len: 1, messages: [{'EXIT',<0.232.0>,normal}], links: [#Port<0.10>,<0.189.0>], dictionary: [], trap_exit: true, status: running, heap_size: 1598, stack_size: 28, reductions: 1505; neighbours:
我做错了什么?我看了很多例子,也在谷歌上搜索了很多例子。我不明白我的代码和互联网上的例子有什么不同。

mlmc2os5

mlmc2os51#

正如上面的评论所述,我发布了两次回应。下面是它应该如何:

json_request(Req, State) ->
  Resp = cowboy_req:set_resp_body(<<"{\"a\":\"b\"}">>, Req),
  cowboy_req:reply(201, Resp),
  {stop, Resp, State}.

确定不好感谢你的评分

相关问题