Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding rest handler demo #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/demos.erl
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ middle() ->
#link { text="Wizard", url="/demos/wizard" },#br{},
#link { text="RESTful Forms", url="/demos/restful" }, #br{},
#link { text="HTML and Custom Encoding", url="/demos/htmlencode"},#br{},
#link { text="Rest API handler", url="/demos/rest"}, #br{},
% #link { text="Recaptcha", url="/demos/recaptcha"},#br{},

#h2 { text="Drag, Drop & Sort" },
Expand Down
50 changes: 50 additions & 0 deletions src/demos/demos_rest.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
-module (demos_rest).
-include_lib ("nitrogen_core/include/wf.hrl").
-compile(export_all).

main() -> #template { file="./templates/demos46.html" }.

title() -> "Rest handler example".

headline() -> "HTTP REST Handler".

left() ->
[
"
<p>
Nitrogen allows the creation of HTTP REST API endpoints.

<p>
It is possible to use various HTTP methods to request and recieves responses through Nitrogen. Methods are provided for decoding & encoding json. Though you can use which ever JSON client you prefair.

<p>
You can use curl or any your favorite HHTP client such as postman..
"
,
linecount:render()
].

right() ->
[
"
<p>
You can test this using the path /demos/rest/api via these methods:

<p>
<h3> GET </h3><br>

curl -X GET \"http://nitrogenproject.com/demos/rest/api/name=m3rl1n&city=london\"

<p>
<h3> POST </h3><br>

curl -X POST \"http://nitrogenproject.com/demos/rest/api\" -d '{\"name\": \"th31nitiate\", \"city\": \"London\"}'

<p>
You can send similar posts request using your favorite HTTP clients such as Postman.
"
].

event(_) -> ok.


24 changes: 24 additions & 0 deletions src/demos/demos_rest_api.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
%% -*- mode: nitrogen -*-
-module (demos_rest_api).
-compile(export_all).
-include_lib("nitrogen_core/include/wf.hrl").
-behavior(nitrogen_rest).

get("") ->
Resp = io_lib:format("A GET request has been submitted with the parameters: ~p~n", [wf:params()]),
{200, Resp}.

post("") ->
%% The best way to post data is via the request body funcation
Params = wf:request_body(),
JsonPayload = wf:json_decode(Params),
Body = io_lib:format("A POST request has been submitted with the parameters: ~p~n", [JsonPayload]),
{200, Body}.

put("") ->
Body = io_lib:format("A PUT request has been submitted with the parameters: ~p~n", ["N/A"]),
{200, Body}.

delete("") ->
Body = io_lib:format("A DELETE request has been submitted with the parameters: ~p~n", ["N/A"]),
{200, Body}.