如何将正文设置为 Cowboy POST 响应
Posted
技术标签:
【中文标题】如何将正文设置为 Cowboy POST 响应【英文标题】:How to set body to Cowboy POST response 【发布时间】:2021-01-05 02:29:13 【问题描述】:我有简单的牛仔休息处理程序:
-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 请求时,在日志中看到错误:
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:
我做错了什么?我查看了示例并在 Google 上搜索了示例。我不明白我的代码与 Internet 上的示例有何不同。
【问题讨论】:
我不是 100% 确定,但是……看起来你回复了两次。你试过从 json_request/2 返回 false, Resp, State 吗? 没有,没试过。谢谢。我现在就试试。 false, Resp, State 来自 json_request/2 - 没有帮助。 【参考方案1】:如上面评论中所述,我发布了两次回复。 下面是它的样子:
json_request(Req, State) ->
Resp = cowboy_req:set_resp_body(<<"\"a\":\"b\"">>, Req),
cowboy_req:reply(201, Resp),
stop, Resp, State.
非常感谢您的帮助。
【讨论】:
以上是关于如何将正文设置为 Cowboy POST 响应的主要内容,如果未能解决你的问题,请参考以下文章
Cowboy:你如何呈现来自处理程序的静态 html 文件?
在 user_data 中为 cowboy_metrics_h 设置元数据
在使用 Hive 进行颤振时,您如何将自定义对象从 POST 响应正文添加到框?
HttpServletResponse ServletResponse 返回响应 设置响应头设置响应正文体 重定向 常用方法 如何重定向 响应编码 响应乱码