[99s-extend] Accept header in POST request

Łukasz Biedrycki lukasz.biedrycki at gmail.com
Mon Feb 3 19:13:04 CET 2014


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ninenines.eu/archives/extend/attachments/20140203/104f8577/attachment.html>


More information about the Extend mailing list