From 2e5e5af71e2a431b47d4868cfdf0fbefa0afd3e5 Mon Sep 17 00:00:00 2001 From: David M3rl1N Jeche Date: Mon, 10 Jul 2023 18:33:15 +0100 Subject: [PATCH] Adding rest handler demo --- src/demos.erl | 1 + src/demos/demos_rest.erl | 50 ++++++++++++++++++++++++++++++++++++ src/demos/demos_rest_api.erl | 24 +++++++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 src/demos/demos_rest.erl create mode 100644 src/demos/demos_rest_api.erl diff --git a/src/demos.erl b/src/demos.erl index 860ac9f..91f8599 100644 --- a/src/demos.erl +++ b/src/demos.erl @@ -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" }, diff --git a/src/demos/demos_rest.erl b/src/demos/demos_rest.erl new file mode 100644 index 0000000..bed5999 --- /dev/null +++ b/src/demos/demos_rest.erl @@ -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() -> + [ + " +

+ Nitrogen allows the creation of HTTP REST API endpoints. + +

+ 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. + +

+ You can use curl or any your favorite HHTP client such as postman.. + " + , + linecount:render() + ]. + +right() -> + [ + " +

+ You can test this using the path /demos/rest/api via these methods: + +

+

GET


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

+

POST


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

+ You can send similar posts request using your favorite HTTP clients such as Postman. + " + ]. + +event(_) -> ok. + + diff --git a/src/demos/demos_rest_api.erl b/src/demos/demos_rest_api.erl new file mode 100644 index 0000000..7fa0cb5 --- /dev/null +++ b/src/demos/demos_rest_api.erl @@ -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}.