2

如何在我的 REST POST 资源中设置状态代码。

现在我正在这样做

make_post(Req, State) ->
  lager:info("post"),
  Resp = cowboy_req:set_resp_body(<<"HELLO POST\n">>, Req),
  {ok, Resp3} = cowboy_req:reply(201, Resp),
  {true, Resp3, State}.

但我收到这样的错误:

1:43:10.510 [info] [nonode@nohost#pbshare_logic_registration#<0.205.0>] [handle_info#35]:从 <0.204.0> 退出逻辑原因:{function_clause,[{cowboy_req,reply,[204,[ ],<<>>,{http_req,#Port<0.6355>,ranch_tcp,keepalive,<0.204.0>,<<"POST">>,'HTTP/1.1',{{127,0,0,1} ,50024},<<"localhost">>,undefined,8080,<<"/rest/registration/make">>,[<<"make">>],<<>>,undefined,[],[ {<<"user-agent">>,<<"curl/7.29.0">>},{<<"host">>,<<"localhost:8080">>},{<<"accept" >>,<<" / ">>},{<<"content-type">>,<<"application/json">>},{<<"内容长度">>,<<"14">>}],[{<<"内容长度">>,14},{<<"预期">>,未定义},{<<"内容-length">>,14},{<<"content-type">>,{<<"application">>,<<"json">>,[]}},{<<"if-modified-因为">>,undefined},{<<"if-none-match">>,undefined},{<<"if-unmodified-since">>,undefined},{<<"if-match">> ,未定义},{<<"接受">>,[{{<<">,{<<"application">>,<<"json">>,[]}},{<<"if-modified-since">>,undefined},{<<"if-none-match" >>,undefined},{<<"if-unmodified-since">>,undefined},{<<"if-match">>,undefined},{<<"accept">>,[{{<< ">,{<<"application">>,<<"json">>,[]}},{<<"if-modified-since">>,undefined},{<<"if-none-match" >>,undefined},{<<"if-unmodified-since">>,undefined},{<<"if-match">>,undefined},{<<"accept">>,[{{<< "">>,<<"">>,[]},1000,[]}]}],undefined,[{charset,undefined},{media_type,{<<"application">>,<<"json">>,[]}} ],done,undefined,<<>>,false,done,[],<<>>,undefined}],[{file,"src/cowboy_req.erl"},{line,948}]},{cowboy_rest ,respond,3,[{file,"src/cowboy_rest.erl"},{line,1085}]},{cowboy_rest,upgrade,4,[{file,"src/cowboy_rest.erl"},{line,75 }]},{cowboy_protocol,execute,4,[{file,"src/cowboy_protocol.erl"},{line,523}]}]} 21:43:10.510 [错误] [Undefined#Undefined#emulator] [Undefined #Undefined] :进程 <0.204.0> 中的错误,退出值:{function_clause,[{cowboy_req,reply,[204,[],<<0 bytes>>,{http_req,#Port<0.6355>,ranch_tcp,keepalive ,<0.204.0>,<<4 bytes>>,'HTTP/1.1',{{127,0,0,1},50024},<<9 bytes>>,undefined,8080,<<23 bytes>>,[<<4 bytes>>],<<0 bytes>>,undefined,[],[{<<10 bytes>>,<<11 bytes>>} ,{<<4 字节>>,<<14 字节>>},{<<6 字节>>,<<3 字节>>},{<<12 字节>>,<<16 字节>>},{ <<14 字节>>,<<2 字节>>}],[{<<14 字节>>,14},{<<6 字节>>,未定义},{<<14 字节>>,14}, {<<12 字节>>,{<<11 字节>>,<<4 字节>>,[]}},{<<17 字节>>,未定义},{<<13 字节>>,未定义}, {<<19 字节>>,未定义},{<<8 字节>>,未定义...<4 字节>>,<<14 字节>>},{<<6 字节>>,<<3 字节>>},{<<12 字节>>,<<16 字节>>},{<<14字节>>,<<2 字节>>}],[{<<14 字节>>,14},{<<6 字节>>,未定义},{<<14 字节>>,14},{<< 12 字节>>,{<<11 字节>>,<<4 字节>>,[]}},{<<17 字节>>,未定义},{<<13 字节>>,未定义},{<< 19 字节>>,未定义},{<<8 字节>>,未定义...<4 字节>>,<<14 字节>>},{<<6 字节>>,<<3 字节>>},{<<12 字节>>,<<16 字节>>},{<<14字节>>,<<2 字节>>}],[{<<14 字节>>,14},{<<6 字节>>,未定义},{<<14 字节>>,14},{<< 12 字节>>,{<<11 字节>>,<<4 字节>>,[]}},{<<17 字节>>,未定义},{<<13 字节>>,未定义},{<< 19 字节>>,未定义},{<<8 字节>>,未定义...11 字节>>,<<4 字节>>,[]}},{<<17 字节>>,未定义},{<<13 字节>>,未定义},{<<19 字节>>,未定义}, {<<8 字节>>,未定义...11 字节>>,<<4 字节>>,[]}},{<<17 字节>>,未定义},{<<13 字节>>,未定义},{<<19 字节>>,未定义}, {<<8 字节>>,未定义...

在我回复之后,牛仔似乎再次尝试发送回复。但是我在 API 中没有任何其他可以更改状态代码的方法。

4

2 回答 2

8

正确的解决方案是返回 not {true, Resp3, State}。但是 {halt, Resp3, State}。在这种情况下,cowboy 不会继续处理请求。

于 2013-06-12T18:38:41.077 回答
2

Cowboy 的 rest-handler 将尝试根据回调函数的返回值设置响应状态码(参见http://ninenines.eu/docs/en/cowboy/HEAD/guide/rest_handlers)。

为了表明一个资源已经被创建(返回 201),除了 body-creating 函数之外,你还需要实现 resource_exists 的回调。您还需要在 body-creating 函数中指明新创建资源的 url。

以下是我认为对您有用的内容(请记住导出resource_exists或将使用默认功能):

resource_exists(Req, State) ->
    {false, Req, State}.

make_post(Req, State) ->
    Req2 = cowboy_req:set_resp_body(<<"Hello world">>, Req),
    {{true, "/foo/bar"}, Req2, State}.
于 2013-06-11T20:59:12.747 回答