1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<tt>
<div dir="ltr">Hi,<div>I have a rest handler that accepts POST and PUT requests with “application/json” content type. </div><div><br></div><div>I have content_types_accepted function defined as follows:</div><div><br></div><br>
<br>
<div><pre style="font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;margin-top:0px;margin-bottom:0px;color:rgb(51,51,51);line-height:18px"><div class="" id="LC93" style="padding-left:10px"><span class="" style="color:rgb(153,0,0);font-weight:bold">content_types_accepted</span><span class="" style>(</span><span class="" style="color:teal">Req</span><span class="" style>,</span> <span class="" style="color:teal">State</span><span class="" style>)</span> <span class="" style="font-weight:bold">-></span></div><br>
<br>
<div class="" id="LC94" style="padding-left:10px"> <span class="" style>{[{</span><span class="" style>‘application/json'</span><span class="" style>,</span> <span class="" style>from_json</span><span class="" style>}],</span> <span class="" style="color:teal">Req</span><span class="" style>,</span> <span class="" style="color:teal">State</span><span class="" style>}.</span></div><br>
<br>
</pre></div><div><br></div><div>The problem I have is within a request that has two headers:</div><div><br></div><div><b>Content-type</b>: application/json</div><div><b>Accept</b>: application/json</div><div><br></div><div><br>
<br>
With this combination I receive <b>406</b>. </div><div><br></div><div>You can repeat it with test:</div><div><br></div><div>http_SUITE.erl:<br></div><div><div>1072 rest_postonly(Config) -></div><div>1073 Client = ?config(client, Config),</div><br>
<br>
<div>1074 Headers = [</div><div>1075 {<<"content-type">>, <<"text/plain">>},</div><div>1076 {<<"accept">>, <<"text/plain">>}</div><br>
<br>
<div>1077 ],</div><div>1078 {ok, Client2} = cowboy_client:request(<<"POST">>,</div><div>1079 build_url("/postonly", Config), Headers, "12345", Client),</div><div><br>
1080 {ok, 204, _, _} = cowboy_client:response(Client2).</div><br>
</div><div><br></div><div>My solution to that was to add a content_types_provided function:</div><div><br></div><div><pre style="font-family:Consolas,'Liberation Mono',Courier,monospace;font-size:12px;margin-top:0px;margin-bottom:0px;color:rgb(51,51,51);line-height:18px"><br>
<br>
<div class="" id="LC108" style="padding-left:10px"><span class="" style="color:rgb(153,0,0);font-weight:bold">content_types_provided</span><span class="" style>(</span><span class="" style="color:teal">Req</span><span class="" style>,</span> <span class="" style="color:teal">State</span><span class="" style>)</span> <span class="" style="font-weight:bold">-></span></div><br>
<br>
<div class="" id="LC109" style="padding-left:10px"> <span class="" style="color:teal">ContentTypes</span> <span class="" style="font-weight:bold">=</span> <span class="" style>[{{</span><span class="" style="font-weight:bold"><<</span><span class="" style="color:rgb(221,17,68)">"application"</span><span class="" style="font-weight:bold">>></span><span class="" style>,</span> <span class="" style="font-weight:bold"><<</span><span class="" style="color:rgb(221,17,68)">"json"</span><span class="" style="font-weight:bold">>></span><span class="" style>,</span> <span class="" style>'*'</span><span class="" style>},</span> <span class="" style>to_json</span><span class="" style>}],</span></div><br>
<br>
<div class="" id="LC110" style="padding-left:10px"> <span class="" style>{</span><span class="" style="color:teal">ContentTypes</span><span class="" style>,</span> <span class="" style="color:teal">Req</span><span class="" style>,</span> <span class="" style="color:teal">State</span><span class="" style>}.</span></div><br>
<br>
</pre></div><div><br></div><div>But it is useless as <b>to_json</b> callback registered is not called anyhow.</div><div><br></div><div>Adding <b>content_types_provided</b> function is a correct solution in this case?</div><br>
<br>
<div>Or I am missing something here?</div><div>“Accept” header is not relevant only in case of GET requests?</div><div><br></div><div>Thank for help,</div><div>�ukasz Biedrycki</div></div><br>
</tt>
|