Web (server and client)
Description
Module that allows you to make small web servers and clients.
Import
(import "web")
Usage
Server
-- Built for web client from examples
(import "std" "web" "json")
(defn greet (fn ln) (concat "Hello, " fn " " ln "!"))
- Create a handler
(defn f (fn ln) (encode $ dict
(first_name fn)
(last_name ln)
(greeting $ greet fn ln)))
(defn fpost (txt fn ln) (concat txt " " fn " " ln))
(defn echo (body) body)
- Build + serve
(serve
$ routeGET "/greet" (list "fname" "lname") f
$ routePOST "/post" (list "fname" "lname") fpost
$ routePOST "/echo" () echo
$ server 2025)
Client
-- Uses web server from examples
(import "web" "std" "io" "json")
- Build request
(set rget $ send
$ withParam "fname" "John"
$ withParam "lname" "Black"
$ get "http://localhost:2025/greet")
- Print response
(print $ concat "Got response: " $ show rget)
(print "Body:")
(print $ resBody rget)
- Decode JSON and get a greeting
(set gr $ lookup greeting $ fst $ decode $ resBody rget)
(print "Greeting:")
(print gr)
Reference
Server
(server <number:port>)
-- creates a web server on portport
.(routeGET <string:path> <list of strings:params> <function(params -> string):handler> <server>)
-- adds GET route toserver
.(routePOST <string:path> <list of strings:params> <function(body, params -> string):handler> <server>)
-- adds POST route toserver
.(serve <server>)
-- serves a server.
Client
(get <string:url>)
-- creates a GET request tourl
.(post <string:url>)
-- creates a POST request tourl
.(withParam <string:name> <string or list of strings:value(s)>)
-- adds param to request.(withBody <string:body>)
-- adds body to request.(send <request>)
-- sendsrequest
and returns response.(resStatus <response>)
-- returns statusCode ofresponse
.(resBody <response>)
-- returns body ofresponse
.