Next: , Previous: , Up: Usage Examples   [Contents][Index]


4.6 POST Echo

The following example echos back the content of the “message” field in a POST request.

;;; post-echo.el --- echo back posted message using Emacs Web Server
(ws-start
 '(((:POST . ".*") .
    (lambda (request)
      (with-slots (process headers) request
        (let ((message (cdr (assoc "message" headers))))
          (ws-response-header process 200 '("Content-type" . "text/plain"))
          (process-send-string process
            (if message
                (format "you said %S\n" (cdr (assoc 'content message)))
              "This is a POST request, but it has no \"message\".\n"))))))
   ((:GET . ".*") .
    (lambda (request)
      (with-slots (process) request
        (ws-response-header process 200 '("Content-type" . "text/plain"))
        (process-send-string process
          "This is a GET request not a POST request.\n")))))
 9005)