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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE> [99s-extend] Accept header in POST request
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20Accept%20header%20in%20POST%20request&In-Reply-To=%3CCAKfrUsiZaAt3k0hDcP2rzUs%3Dw0mTLF7TH3fo%2B9oLbDu7i0sq-g%40mail.gmail.com%3E">
<META NAME="robots" CONTENT="index,nofollow">
<style type="text/css">
pre {
white-space: pre-wrap; /* css-2.1, curent FF, Opera, Safari */
}
</style>
<META http-equiv="Content-Type" content="text/html; charset=us-ascii">
<LINK REL="Previous" HREF="000325.html">
<LINK REL="Next" HREF="000327.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[99s-extend] Accept header in POST request</H1>
<B>Łukasz Biedrycki</B>
<A HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20Accept%20header%20in%20POST%20request&In-Reply-To=%3CCAKfrUsiZaAt3k0hDcP2rzUs%3Dw0mTLF7TH3fo%2B9oLbDu7i0sq-g%40mail.gmail.com%3E"
TITLE="[99s-extend] Accept header in POST request">lukasz.biedrycki at gmail.com
</A><BR>
<I>Mon Feb 3 19:26:17 CET 2014</I>
<P><UL>
<LI>Previous message: <A HREF="000325.html">[99s-extend] Accept header in POST request
</A></li>
<LI>Next message: <A HREF="000327.html">[99s-extend] Accept header in POST request
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#326">[ date ]</a>
<a href="thread.html#326">[ thread ]</a>
<a href="subject.html#326">[ subject ]</a>
<a href="author.html#326">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>My application sends both headers: “Content-type” and “Accept” header using
POST method.
For POST rest handler do I have to specify both: content_types_accepted and
content_types_provided to manage this kind of request?
On Mon, Feb 3, 2014 at 7:23 PM, Loïc Hoguin <<A HREF="https://lists.ninenines.eu/listinfo/extend">essen at ninenines.eu</A>> wrote:
><i> The content-type provided is relevant for any response, not just responses
</I>><i> to GET requests. It defaults to text/html. If your client doesn't send that
</I>><i> content-type, you have to define the callback.
</I>><i>
</I>><i> I notice that the documentation is incorrect about the relevant methods
</I>><i> for this callback, I will open a ticket to fix it soon.
</I>><i>
</I>><i>
</I>><i> On 02/03/2014 07:13 PM, Łukasz Biedrycki wrote:
</I>><i>
</I>>><i> Hi,
</I>>><i> I have a rest handler that accepts POST and PUT requests with
</I>>><i> “application/json” content type.
</I>>><i>
</I>>><i> I have content_types_accepted function defined as follows:
</I>>><i>
</I>>><i> content_types_accepted(Req, State) ->
</I>>><i>
</I>>><i>
</I>>><i> {[{‘application/json', from_json}], Req, State}.
</I>>><i>
</I>>><i>
</I>>><i>
</I>>><i> The problem I have is within a request that has two headers:
</I>>><i>
</I>>><i> *Content-type*: application/json
</I>>><i> *Accept*: application/json
</I>>><i>
</I>>><i> With this combination I receive *406*.
</I>>><i>
</I>>><i>
</I>>><i> You can repeat it with test:
</I>>><i>
</I>>><i> http_SUITE.erl:
</I>>><i> 1072 rest_postonly(Config) ->
</I>>><i> 1073 Client = ?config(client, Config),
</I>>><i> 1074 Headers = [
</I>>><i> 1075 {<<"content-type">>, <<"text/plain">>},
</I>>><i> 1076 {<<"accept">>, <<"text/plain">>}
</I>>><i> 1077 ],
</I>>><i> 1078 {ok, Client2} = cowboy_client:request(<<"POST">>,
</I>>><i> 1079 build_url("/postonly", Config), Headers, "12345", Client),
</I>>><i> 1080 {ok, 204, _, _} = cowboy_client:response(Client2).
</I>>><i>
</I>>><i> My solution to that was to add a content_types_provided function:
</I>>><i>
</I>>><i>
</I>>><i> content_types_provided(Req, State) ->
</I>>><i>
</I>>><i>
</I>>><i> ContentTypes = [{{<<"application">>, <<"json">>, '*'}, to_json}],
</I>>><i>
</I>>><i>
</I>>><i> {ContentTypes, Req, State}.
</I>>><i>
</I>>><i>
</I>>><i>
</I>>><i> But it is useless as *to_json* callback registered is not called anyhow.
</I>>><i>
</I>>><i> Adding *content_types_provided* function is a correct solution in this
</I>>><i> case?
</I>>><i>
</I>>><i> Or I am missing something here?
</I>>><i> “Accept” header is not relevant only in case of GET requests?
</I>>><i>
</I>>><i> Thank for help,
</I>>><i> Łukasz Biedrycki
</I>>><i>
</I>>><i>
</I>>><i> _______________________________________________
</I>>><i> Extend mailing list
</I>>><i> <A HREF="https://lists.ninenines.eu/listinfo/extend">Extend at lists.ninenines.eu</A>
</I>>><i> <A HREF="https://lists.ninenines.eu/listinfo/extend">https://lists.ninenines.eu/listinfo/extend</A>
</I>>><i>
</I>>><i>
</I>><i> --
</I>><i> Loïc Hoguin
</I>><i> <A HREF="http://ninenines.eu">http://ninenines.eu</A>
</I>><i>
</I>-------------- next part --------------
An HTML attachment was scrubbed...
URL: <<A HREF="http://lists.ninenines.eu/archives/extend/attachments/20140203/2982cff3/attachment.html">http://lists.ninenines.eu/archives/extend/attachments/20140203/2982cff3/attachment.html</A>>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="000325.html">[99s-extend] Accept header in POST request
</A></li>
<LI>Next message: <A HREF="000327.html">[99s-extend] Accept header in POST request
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#326">[ date ]</a>
<a href="thread.html#326">[ thread ]</a>
<a href="subject.html#326">[ subject ]</a>
<a href="author.html#326">[ author ]</a>
</LI>
</UL>
<hr>
<a href="https://lists.ninenines.eu/listinfo/extend">More information about the Extend
mailing list</a><br>
</body></html>
|