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
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE> [99s-extend] question to rest handler
</TITLE>
<LINK REL="Index" HREF="index.html" >
<LINK REL="made" HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20question%20to%20rest%20handler&In-Reply-To=%3C519657B3.1080104%40ninenines.eu%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="000143.html">
<LINK REL="Next" HREF="000145.html">
</HEAD>
<BODY BGCOLOR="#ffffff">
<H1>[99s-extend] question to rest handler</H1>
<B>Loïc Hoguin</B>
<A HREF="mailto:extend%40lists.ninenines.eu?Subject=Re%3A%20%5B99s-extend%5D%20question%20to%20rest%20handler&In-Reply-To=%3C519657B3.1080104%40ninenines.eu%3E"
TITLE="[99s-extend] question to rest handler">essen at ninenines.eu
</A><BR>
<I>Fri May 17 18:15:47 CEST 2013</I>
<P><UL>
<LI>Previous message: <A HREF="000143.html">[99s-extend] question to rest handler
</A></li>
<LI>Next message: <A HREF="000145.html">[99s-extend] Cowboy Middleware and websockets
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#144">[ date ]</a>
<a href="thread.html#144">[ thread ]</a>
<a href="subject.html#144">[ subject ]</a>
<a href="author.html#144">[ author ]</a>
</LI>
</UL>
<HR>
<!--beginarticle-->
<PRE>All the callbacks are explained in
<A HREF="http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest">http://ninenines.eu/docs/en/cowboy/HEAD/manual/cowboy_rest</A>
In your case take a special look at content_types_accepted.
On 05/17/2013 05:41 PM, Witali Monastyrjow wrote:
><i> Hi all,
</I>><i>
</I>><i> I am learning cowboy by building a small application with rest interface.
</I>><i> I have a hello_world rest handler and I want to implement POST method
</I>><i> that returns
</I>><i> json as response to a client. Therefor I implemented callbacks
</I>><i> allowed_methods,
</I>><i> content_types_accepted and hello_json. The docu says user callbacks can
</I>><i> return {Value, Req, State} and also can return {halt, Req, State}. It is
</I>><i> not really clear
</I>><i> what that Value should be. So I tried {ok, Req, State} and {true, Req,
</I>><i> State} and with
</I>><i> both values I have
</I>><i>
</I>><i> =ERROR REPORT==== 11-May-2013::16:06:40 ===
</I>><i> Error in process <0.6649.0> with exit value:
</I>><i> {function_clause,[{cowboy_req,reply,[303,....
</I>><i>
</I>><i> and client gets right response. If I use {halt, Req, State} the client
</I>><i> gets right response too
</I>><i> and there is no errors. So, Is it right way to write a POST callback and
</I>><i> what Values can
</I>><i> be used for user callbacks? I write my code below.
</I>><i>
</I>><i> amike,
</I>><i> Vitali
</I>><i>
</I>><i> allowed_methods(Req, State) ->
</I>><i> {[<<"POST">>, <<"DELETE">>], Req, State}.
</I>><i>
</I>><i> content_types_accepted(Req, State) ->
</I>><i> {[
</I>><i> {{<<"application">>, <<"x-www-form-urlencoded">>, []}, hello_json}
</I>><i> ], Req, State}.
</I>><i>
</I>><i> hello_json(Req, State) ->
</I>><i> {ok, Req2} = cowboy_req:reply(200, [{<<"content-type">>,
</I>><i> <<"application/json">>} ], <<"{\"rest\": \"Hello World!\"}">>, Req),
</I>><i> {halt, Req2, State}.
</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="http://lists.ninenines.eu:81/listinfo/extend">http://lists.ninenines.eu:81/listinfo/extend</A>
</I>><i>
</I>
--
Loïc Hoguin
Erlang Cowboy
Nine Nines
<A HREF="http://ninenines.eu">http://ninenines.eu</A>
</PRE>
<!--endarticle-->
<HR>
<P><UL>
<!--threads-->
<LI>Previous message: <A HREF="000143.html">[99s-extend] question to rest handler
</A></li>
<LI>Next message: <A HREF="000145.html">[99s-extend] Cowboy Middleware and websockets
</A></li>
<LI> <B>Messages sorted by:</B>
<a href="date.html#144">[ date ]</a>
<a href="thread.html#144">[ thread ]</a>
<a href="subject.html#144">[ subject ]</a>
<a href="author.html#144">[ author ]</a>
</LI>
</UL>
<hr>
<a href="https://lists.ninenines.eu/listinfo/extend">More information about the Extend
mailing list</a><br>
</body></html>
|