awful JSON payload added by mario-goulart on Fri May 29 11:36:59 2020

;; $ curl -X POST http://localhost:8080/read-json -d '{"foo": "xx", "bar": "yy"}'
;; #(("foo" . "xx") ("bar" . "yy"))


(import (chicken io)
        (chicken port)
        (chicken pretty-print))
(import awful json intarweb spiffy)

(define (read-json-from-request)
  (let* ((req (current-request))
         (headers (request-headers (current-request)))
         (content-length (header-value 'content-length headers)))
    (cond ((and content-length (zero? content-length))
           "")
          ((not content-length)
           (error 'echo-service "Set content-length, sloppy client"))
          (else (call-with-input-string
                 (read-string content-length  (request-port req))
                 json-read)))))

(define-page "/read-json"
  (lambda ()
    `(literal
      ,(with-output-to-string
         (lambda ()
           (pp (read-json-from-request))))))
  no-template: #t
  method: 'POST)