The code on this page is ideal for web services, where you need to interface to the web, but you don’t really need HTML rendering. Just as HttpGet makes HTTP GET requests, the code on this page makes HTTP POST requests.
See also HttpGet.
The UrlPackage which is now part of Emacs, provides support for authentication and cookies and can do POST requests, for example:
(let ((url-request-method "POST") (url-request-extra-headers `(("Content-Type" . "application/x-www-form-urlencoded"))) (url-request-data "field1=Hello&field2=from&field3=Emacs")) (with-current-buffer (url-retrieve-synchronously url) (buffer-string)))
I also have made some enhancement: insert the HTTP output in a configurable buffer, instead of mixed in \*Message\* buffer.
Since I can’t contact with the original author (Tom Schutzer-Weissmann), I post my enhancement here.
(defun http-post-simple (url fields &optional charset bufname) "Send FIELDS to URL as an HTTP POST request, returning the response and response headers. FIELDS is an alist, eg ((field-name . \"value\")); all values need to be strings, and they are encoded using CHARSET, which defaults to 'utf-8" (let (request-result) (setq request-result (http-post-simple-internal url (http-post-encode-fields fields charset) charset `(("Content-Type" . ,(http-post-content-type "application/x-www-form-urlencoded" charset))))) ;; set buffername (unless bufname (setq bufname (format "*HTTP POST %s *" url))) (save-excursion (delete-other-windows) ;; bury other window (split-window-vertically) ;; split window vertically ;; switch to window for HTTP output (pop-to-buffer (get-buffer-create bufname)) (insert (mapconcat '(lambda(x) (if (integerp x) (int-to-string x) (identity x))) (nreverse request-result) "\n")) (beginning-of-buffer)) ))
;; -*- lexical-binding: t -*- (require 'web) (defun my-web-post-done (result) (message "the web post is done! %S" result)) (let ((query-data (make-hash-table :test 'equal))) (puthash 'name "nic" query-data) (puthash 'email "email@example.com" query-data) (web-http-post (lambda (con header data) (my-web-post-done data)) "http://localhost:8001/someplace" :data query-data))
It’s available from marmalade here.