[99s-extend] cowboy_rest and delete_completed and response

Stéphane Wirtel stephane at wirtel.be
Wed Aug 27 00:12:00 CEST 2014


What's the purpose of the callbacks in content_types_accepted and 
content_types_provided?

I prefer set the response in the State to the callbacks and they convert 
it to the right format.

Example:

delete_completed(Req, State) ->
	Response = [{<<"ok">>, <<"dbname">>}],
	{true, Req, State#state{response=Response}}.

get_json(Req, #{response=Response}=State) ->
	Body = jsx:encode(Response),
	{Body, Req, State}.

get_msgpack(Req, #{response=Response}=State) ->
	Body = msgpack:pack(Response, [{format, jsx}],
	{Body, Req, State}.



On 27 Aug 2014, at 0:03, Loïc Hoguin wrote:

> Call cowboy_req:meta(media_type, Req) to retrieve it.
>
> On 08/27/2014 12:59 AM, Stéphane Wirtel wrote:
>> Hi all,
>>
>> I work with two content-types (json, msgpack).
>>
>> In the DELETE verb, I need to return an object and in this case, I 
>> work
>> on delete_resource/2 and delete_completed/2.
>> The problem is, how can I return a body in function of the 
>> content-type?
>> because after delete_completed, there is a call to the
>> cowboy_rest:has_resp_body function and I need to set the body of the
>> response.
>>
>> delete_completed(Req, State) ->
>>   Body = Json or MsgPack ?  <-- Which content ?
>>
>>   Req2 = cowboy_req:set_resp_body(Body, Req),
>>   {true, Req2, State}.
>>
>> Ok, but in this case, what's the reason of content_types_provided/2 
>> and
>> content_types_accepted/2 ?
>>
>> Thank you,
>>
>> Stephane
>>
>>
>> --
>> Stéphane Wirtel - http://wirtel.be - @matrixise
>> _______________________________________________
>> Extend mailing list
>> Extend at lists.ninenines.eu
>> https://lists.ninenines.eu/listinfo/extend
>
> -- 
> Loïc Hoguin
> http://ninenines.eu


--
Stéphane Wirtel - http://wirtel.be - @matrixise


More information about the Extend mailing list