[99s-extend] question to rest handler

Loïc Hoguin essen at ninenines.eu
Fri May 17 18:15:47 CEST 2013


All the callbacks are explained in 
http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest

In your case take a special look at content_types_accepted.

On 05/17/2013 05:41 PM, Witali Monastyrjow wrote:
> Hi all,
>
> I am learning cowboy by building a small application with rest interface.
> I have a hello_world rest handler and I want to implement POST method
> that returns
> json as response to a client. Therefor I implemented  callbacks
> allowed_methods,
> content_types_accepted and hello_json. The docu says user callbacks can
> return {Value, Req, State} and also can return {halt, Req, State}. It is
> not really clear
> what that Value should be. So I tried {ok, Req, State} and {true, Req,
> State} and with
> both values I have
>
> =ERROR REPORT==== 11-May-2013::16:06:40 ===
> Error in process <0.6649.0> with exit value:
> {function_clause,[{cowboy_req,reply,[303,....
>
> and client gets right response. If I use {halt, Req, State} the client
> gets right response too
> and there is no errors. So, Is it right way to write a POST callback and
> what Values can
> be used for user callbacks? I write my code below.
>
> amike,
> Vitali
>
> allowed_methods(Req, State) ->
> {[<<"POST">>, <<"DELETE">>], Req, State}.
>
> content_types_accepted(Req, State) ->
> {[
>      {{<<"application">>, <<"x-www-form-urlencoded">>, []}, hello_json}
>   ], Req, State}.
>
> hello_json(Req, State) ->
> {ok, Req2} = cowboy_req:reply(200, [{<<"content-type">>,
> <<"application/json">>} ], <<"{\"rest\": \"Hello World!\"}">>, Req),
> {halt, Req2, State}.
>
>
> _______________________________________________
> Extend mailing list
> Extend at lists.ninenines.eu
> http://lists.ninenines.eu:81/listinfo/extend
>


-- 
Loïc Hoguin
Erlang Cowboy
Nine Nines
http://ninenines.eu



More information about the Extend mailing list