[erlang-questions] Cowboy REST POST and custom status code

Phillips, Christopher Christopher.Phillips@REDACTED
Wed Jun 12 15:53:44 CEST 2013


  I'm not looking too closely at the errors, so this may not be all you
need, but, all of the REST callbacks can return a tuple of {halt,
Response, State}, which is the correct idiom to use when you want to stop
the REST handler from continuing (so in your code you'd return {halt,
Resp3, State}).

  That said, you may want to look at the REST handler's flow logic; there
is a path that will automatically return a 201 if you opt to use it (I
don't recall it offhand, and it may be that migrating to use it is more
effort than just halting with a 201).

  If the former doesn't address everything, and the latter isn't feasible
(or you have questions about it), consider signing up for and asking on
the 99s mailing list, extend@REDACTED<mailto:extend@REDACTED>.


On 6/12/13 4:35 AM, "erlang-questions-request@REDACTED<mailto:erlang-questions-request@REDACTED>"
<erlang-questions-request@REDACTED<mailto:erlang-questions-request@REDACTED>> wrote:

How can I set status code in my REST POST handler.

Now I'm doing this

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}.

But I receive such errors:

1:43:10.510 [info] [nonode@REDACTED#pbshare_logic_registration#<0.205.0>]
[handle_info#35] :Exit Logic from <0.204.0> Reason:
{function_clause,[{cowboy_req,reply,[204,[],<<>>,{http_req,#Port<0.6355>,r
anch_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">>,<<"localh
ost:8080">>},{<<"accept">>,<<"
*/*
">>},{<<"content-type">>,<<"application/json">>},{<<"content-length">>,<<"
14">>}],[{<<"content-length">>,14},{<<"expect">>,undefined},{<<"content-le
ngth">>,14},{<<"content-type">>,{<<"application">>,<<"json">>,[]}},{<<"if-
modified-since">>,undefined},{<<"if-none-match">>,undefined},{<<"if-unmodi
fied-since">>,undefined},{<<"if-match">>,undefined},{<<"accept">>,[{{<<"
*">>,<<"*">>,[]},1000,[]}]}],undefined,[{charset,undefined},{media_type,{<
<"application">>,<<"json">>,[]}}],done,undefined,<<>>,false,done,[],<<>>,u
ndefined}],[{file,"src/cowboy_req.erl"},{line,948}]},{cowboy_rest,respond,
3,[{file,"src/cowboy_rest.erl"},{line,1085}]},{cowboy_rest,upgrade,4,[{fil
e,"src/cowboy_rest.erl"},{line,75}]},{cowboy_protocol,execute,4,[{file,"sr
c/cowboy_protocol.erl"},{line,523}]}]}
21:43:10.510 [error] [Undefined#Undefined#emulator] [Undefined#Undefined]
:Error in process <0.204.0> with exit value:
{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 bytes>>,<<14 bytes>>},{<<6 bytes>>,<<3 bytes>>},{<<12
bytes>>,<<16 bytes>>},{<<14 bytes>>,<<2 bytes>>}],[{<<14 bytes>>,14},{<<6
bytes>>,undefined},{<<14 bytes>>,14},{<<12 bytes>>,{<<11 bytes>>,<<4
bytes>>,[]}},{<<17 bytes>>,undefined},{<<13 bytes>>,undefined},{<<19
bytes>>,undefined},{<<8 bytes>>,undefined...

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20130612/a03b498f/attachment.htm>


More information about the erlang-questions mailing list