The code is available for download from:
This code is also making use of the following:Please note that I'm making use of Guile/Scheme, so for other scheme implementation there will be some rework involved specifically for the web server stuff and some of the build in functions that Guile has that other schemes either doesn't have or doesn't implement in exactly the same way.
(load "webserv-support.scm") (define logonstr '((username . "daffy") (password . "duck))) (define logonjson (escm->json logonstr)) ; this will generate {"username": "daffy", "password": "duck"} (define andback (esjson->scm logonjson)) ; this will generate ((username . "daffy") (password . "duck))As can be seen from the examle the conversion is extremely straight forward to either side.
(define (getInput body rh) (response-header-set rh 'content-type '(application/json)) (response-header-set rh 'Access-Control-Allow-Origin "*") (esjson->scm (bytevector->string body "utf-8")))Seeing that JSON objects are converted to association lists in our Guile implementation we can access the fields by assoc-ref. Three helper functions will generate the output (in JSON format) and they are:
(use-modules (web uri)) (use-modules (web client)) (use-modules (ice-9 receive)) (define (callService call body) (let* ((req (string-append "http://localhost:8080/" call)) (tmp (receive (misc data) (http-post req #:body (esscm->json body)) data))) (esjson->scm tmp)))To post the request, the body is first converted to JSON, then http-post is called (part of modules included). This generates a multi-value return of the returned headers and the returned body. receive will catch the multi-values for us, but we are only interested in the body part (data in this case). The returned body is then converted from JSON to scheme and returned.