bryan / wmexamples
Example resources for Webmachine (http://bitbucket.org/justin/webmachine). This project contains several sample webmachine resources that I've posted elsewhere at various times. This is a complete Webmachine app ready to be run and examined.
Clone this repository (size: 42.5 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/bryan/wmexamples/
| commit 13: | a0bb705647f6 |
| parent 12: | 0cc7ce973ccd |
| branch: | default |
add POST example in formjson_resource.erl
10 months ago
Changed (Δ2.0 KB):
raw changeset »
README (15 lines added, 0 lines removed)
priv/dispatch.conf (10 lines added, 0 lines removed)
src/formjson_resource.erl (36 lines added, 0 lines removed)
| … | … | @@ -86,3 +86,18 @@ which is included and exposed at http:// |
86 |
86 |
That resource isn't good for much other than generating those simple |
87 |
87 |
examples, but I thought it best to include it for demonstration |
88 |
88 |
purposes. |
89 |
||
90 |
POST Example (wwwform -> json) |
|
91 |
--- |
|
92 |
||
93 |
formjson_resource.erl is mainly an example of how one might handle a |
|
94 |
POST in a webmachine resource. process_post/2 demonstrates how to |
|
95 |
grab the request body out of the ReqData (wrq:req_body/1), and push a |
|
96 |
response body into one (wrq:append_to_response_body/2). |
|
97 |
||
98 |
Issuing a GET to formjson_resource will encode the query parameters of |
|
99 |
the request as JSON, and return that encoding in the response body. |
|
100 |
||
101 |
Issuing a POST to formjson_resource will assume that the body of the |
|
102 |
request is application/x-www-form-urlencoded data, re-ecode it as |
|
103 |
JSON, and return that JSON in the response body. |
Up to file-list priv/dispatch.conf:
57 |
57 |
sampletrace_resource, |
58 |
58 |
[]}. |
59 |
59 |
|
60 |
%% x-www-form-urlencoded -> JSON translator |
|
61 |
%% Converts form data to JSON data. |
|
62 |
%% Try: |
|
63 |
%% curl "http://localhost:8000/formjson?one=two&me=pope" |
|
64 |
%% curl -X POST http://localhost:8000/formjson \ |
|
65 |
%% -d "one=two&me=pope" |
|
66 |
{["formjson"], %% exposed at /formjson |
|
67 |
formjson_resource, %% defined in formjson_resource |
|
68 |
[]}. %% no configuration |
|
69 |
||
60 |
70 |
|
61 |
71 |
%% File Resource |
62 |
72 |
%% Serve files from disk. |
Up to file-list src/formjson_resource.erl:
1 |
%% @author Bryan Fink |
|
2 |
%% @doc formjson_resource takes x-www-form-urlencoded data and |
|
3 |
%% re-encodes it as JSON. The data is expected as query |
|
4 |
%% parameters for a GET, and request body for a POST. |
|
5 |
%% |
|
6 |
%% In: one=two&me=pope |
|
7 |
%% Out: {"one":"two","me":"pope"} |
|
8 |
-module(formjson_resource). |
|
9 |
-export([init/1, |
|
10 |
allowed_methods/2, |
|
11 |
content_types_provided/2, |
|
12 |
to_json/2, |
|
13 |
process_post/2]). |
|
14 |
-include_lib("webmachine/include/webmachine.hrl"). |
|
15 |
||
16 |
init(_) -> {ok, undefined}. |
|
17 |
||
18 |
allowed_methods(RD, Ctx) -> |
|
19 |
{['GET', 'HEAD', 'POST'], RD, Ctx}. |
|
20 |
||
21 |
content_types_provided(RD, Ctx) -> |
|
22 |
{[{"application/json", to_json}], RD, Ctx}. |
|
23 |
||
24 |
%% hit this with |
|
25 |
%% curl "http://localhost:8000/formjson?one=two&me=pope" |
|
26 |
to_json(RD, Ctx) -> |
|
27 |
{json_body(wrq:req_qs(RD)), RD, Ctx}. |
|
28 |
||
29 |
%% hit this with |
|
30 |
%% curl -X POST http://localhost:8000/formjson \ |
|
31 |
%% -d "one=two&me=pope" |
|
32 |
process_post(RD, Ctx) -> |
|
33 |
Body = json_body(mochiweb_util:parse_qs(wrq:req_body(RD))), |
|
34 |
{true, wrq:append_to_response_body(Body, RD), Ctx}. |
|
35 |
||
36 |
json_body(QS) -> mochijson:encode({struct, QS}). |
