<div dir="ltr">Perfect, thanks a lot!<div>Cheers,</div><div>Ł.</div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Feb 3, 2014 at 8:15 PM, Loïc Hoguin <span dir="ltr"><<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Sure. It won't be called if not a GET or HEAD request so that's probably the best value you can return in your case.<div class="im">
<br>
<br>
On 02/03/2014 08:08 PM, Łukasz Biedrycki wrote:<br>
</div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
Ok,<br>
it is more clear for me.<br>
<br>
Last question I have is about content_types_provided function.<br>
<br>
Is it safe to define it like this?<br>
<br>
content_types_provided(R, S) -><br></div>
ContentTypes = [{{<<"application">>, <<"json">>, '*'}, *undefined*}],<div class="im"><br>
{ContentTypes, Req, State}.<br>
<br>
Callback in content_types_provided is useless for POST requests, as it<br>
won’t be called.<br></div>
Is it safe to use *undefined *atom, to have a source code clearer?<div class="im"><br>
<br>
<br>
<br>
<br>
On Mon, Feb 3, 2014 at 7:37 PM, Loïc Hoguin <<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a><br></div><div class="im">
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>> wrote:<br>
<br>
If Accept is sent and is different than text/html, yes.<br>
<br>
This is how HTTP is defined. If the client says it speaks only<br>
content-type X but you can only reply with content-type Y, you error<br>
out early and stop processing the request. On the other hand if the<br>
client doesn't say what content-type it speaks then the server can<br>
choose whichever one it wants.<br>
<br>
<br>
On 02/03/2014 07:26 PM, Łukasz Biedrycki wrote:<br>
<br>
My application sends both headers: “Content-type” and “Accept”<br>
header<br>
using POST method.<br>
<br>
For POST rest handler do I have to specify both:<br>
content_types_accepted<br>
and content_types_provided to manage this kind of request?<br>
<br>
<br>
On Mon, Feb 3, 2014 at 7:23 PM, Loïc Hoguin <<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a><br>
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>><br></div><div><div class="h5">
<mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a> <mailto:<a href="mailto:essen@ninenines.eu" target="_blank">essen@ninenines.eu</a>>>> wrote:<br>
<br>
The content-type provided is relevant for any response, not<br>
just<br>
responses to GET requests. It defaults to text/html. If<br>
your client<br>
doesn't send that content-type, you have to define the<br>
callback.<br>
<br>
I notice that the documentation is incorrect about the relevant<br>
methods for this callback, I will open a ticket to fix it soon.<br>
<br>
<br>
On 02/03/2014 07:13 PM, Łukasz Biedrycki wrote:<br>
<br>
Hi,<br>
I have a rest handler that accepts POST and PUT<br>
requests with<br>
“application/json” content type.<br>
<br>
I have content_types_accepted function defined as follows:<br>
<br>
content_types_accepted(Req, State) -><br>
<br>
<br>
{[{‘application/json', from_json}], Req, State}.<br>
<br>
<br>
<br>
The problem I have is within a request that has two<br>
headers:<br>
<br>
*Content-type*: application/json<br>
*Accept*: application/json<br>
<br>
With this combination I receive *406*.<br>
<br>
<br>
You can repeat it with test:<br>
<br>
http_SUITE.erl:<br>
1072 rest_postonly(Config) -><br>
1073 Client = ?config(client, Config),<br>
1074 Headers = [<br>
1075 {<<"content-type">>, <<"text/plain">>},<br>
1076 {<<"accept">>, <<"text/plain">>}<br>
1077 ],<br>
1078 {ok, Client2} =<br></div></div>
cowboy_client:request(<<"POST"<u></u>____>>,<div class="im"><br>
<br>
1079 build_url("/postonly", Config), Headers,<br>
"12345",<br>
Client),<br>
1080 {ok, 204, _, _} =<br></div>
cowboy_client:response(____<u></u>Client2).<div class="im"><br>
<br>
<br>
My solution to that was to add a content_types_provided<br>
function:<br>
<br>
<br>
content_types_provided(Req, State) -><br>
<br>
<br>
ContentTypes = [{{<<"application">>, <<"json">>, '*'},<br>
to_json}],<br>
<br>
<br>
{ContentTypes, Req, State}.<br>
<br>
<br>
<br>
But it is useless as *to_json* callback registered is<br>
not called<br>
anyhow.<br>
<br>
Adding *content_types_provided* function is a correct<br>
solution<br>
in this case?<br>
<br>
Or I am missing something here?<br>
“Accept” header is not relevant only in case of GET<br>
requests?<br>
<br>
Thank for help,<br>
Łukasz Biedrycki<br>
<br>
<br></div>
______________________________<u></u>_____________________<div class="im"><br>
Extend mailing list<br>
<a href="mailto:Extend@lists.ninenines.eu" target="_blank">Extend@lists.ninenines.eu</a> <mailto:<a href="mailto:Extend@lists.ninenines.eu" target="_blank">Extend@lists.<u></u>ninenines.eu</a>><br></div>
<mailto:<a href="mailto:Extend@lists." target="_blank">Extend@lists.</a>__<a href="http://ninenines.eu" target="_blank">ninenin<u></u>es.eu</a><br>
<mailto:<a href="mailto:Extend@lists.ninenines.eu" target="_blank">Extend@lists.<u></u>ninenines.eu</a>>><br>
<a href="https://lists.ninenines.eu/____listinfo/extend" target="_blank">https://lists.ninenines.eu/___<u></u>_listinfo/extend</a><br>
<<a href="https://lists.ninenines.eu/__listinfo/extend" target="_blank">https://lists.ninenines.eu/__<u></u>listinfo/extend</a>><div class="im"><br>
<br>
<<a href="https://lists.ninenines.eu/__listinfo/extend" target="_blank">https://lists.ninenines.eu/__<u></u>listinfo/extend</a><br>
<<a href="https://lists.ninenines.eu/listinfo/extend" target="_blank">https://lists.ninenines.eu/<u></u>listinfo/extend</a>>><br>
<br>
<br>
--<br>
Loïc Hoguin<br>
<a href="http://ninenines.eu" target="_blank">http://ninenines.eu</a><br>
<br>
<br>
<br>
--<br>
Loïc Hoguin<br>
<a href="http://ninenines.eu" target="_blank">http://ninenines.eu</a><br>
<br>
<br>
</div></blockquote><div class="HOEnZb"><div class="h5">
<br>
-- <br>
Loïc Hoguin<br>
<a href="http://ninenines.eu" target="_blank">http://ninenines.eu</a><br>
</div></div></blockquote></div><br></div>