[99s-extend] Accept header in POST request

Loïc Hoguin essen at ninenines.eu
Mon Feb 3 19:23:32 CET 2014


The content-type provided is relevant for any response, not just 
responses to GET requests. It defaults to text/html. If your client 
doesn't send that content-type, you have to define the callback.

I notice that the documentation is incorrect about the relevant methods 
for this callback, I will open a ticket to fix it soon.

On 02/03/2014 07:13 PM, Łukasz Biedrycki wrote:
> Hi,
> I have a rest handler that accepts POST and PUT requests with
> “application/json” content type.
>
> I have content_types_accepted function defined as follows:
>
> content_types_accepted(Req, State) ->
>
>
> {[{‘application/json', from_json}], Req, State}.
>
>
>
> The problem I have is within a request that has two headers:
>
> *Content-type*: application/json
> *Accept*: application/json
>
> With this combination I receive *406*.
>
> You can repeat it with test:
>
> http_SUITE.erl:
> 1072 rest_postonly(Config) ->
> 1073     Client = ?config(client, Config),
> 1074     Headers = [
> 1075         {<<"content-type">>, <<"text/plain">>},
> 1076         {<<"accept">>, <<"text/plain">>}
> 1077     ],
> 1078     {ok, Client2} = cowboy_client:request(<<"POST">>,
> 1079         build_url("/postonly", Config), Headers, "12345", Client),
> 1080     {ok, 204, _, _} = cowboy_client:response(Client2).
>
> My solution to that was to add a content_types_provided function:
>
>
> content_types_provided(Req, State) ->
>
>
> ContentTypes = [{{<<"application">>, <<"json">>, '*'}, to_json}],
>
>
> {ContentTypes, Req, State}.
>
>
>
> But it is useless as *to_json* callback registered is not called anyhow.
>
> Adding *content_types_provided* function is a correct solution in this case?
> Or I am missing something here?
> “Accept” header is not relevant only in case of GET requests?
>
> Thank for help,
> Łukasz Biedrycki
>
>
> _______________________________________________
> Extend mailing list
> Extend at lists.ninenines.eu
> https://lists.ninenines.eu/listinfo/extend
>

-- 
Loïc Hoguin
http://ninenines.eu



More information about the Extend mailing list