Next: Configuration for request-el, Previous: Installation, Up: request [Index]
(request
"http://httpbin.org/get"
:params '(("key" . "value") ("key2" . "value2"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'args data)))))
Listing 13.1: GET Example
(request
"http://httpbin.org/get"
:sync t
:complete (cl-function
(lambda (&key response &allow-other-keys)
(message "Done: %s" (request-response-status-code response)))))
Listing 13.2: Block until completion
(request
"http://httpbin.org/get"
:auth "digest" ;; or "basic", "anyauth", etc., which see curl(1)
:complete (cl-function
(lambda (&key response &allow-other-keys)
(message "Done: %s" (request-response-status-code response)))))
Listing 13.3: Curl Authentication
(request
"http://httpbin.org/get"
:encoding 'binary
:complete (cl-function
(lambda (&key response &allow-other-keys)
(message "Done: %s" (request-response-status-code response)))))
Listing 13.4: Request Binary Data
(request
"http://httpbin.org/post"
:type "POST"
:data '(("key" . "value") ("key2" . "value2"))
;; :data "key=value&key2=value2" ; this is equivalent
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'form data)))))
Listing 13.5: POST Example
(request
"http://httpbin.org/post"
:type "POST"
:files `(("current buffer" . ,(current-buffer))
("data" . ("data.csv" :data "1,2,3\n4,5,6\n")))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'files data)))))
Listing 13.6: POST FILE Example
#caption:Rich callback dispatch
(request
"http://httpbin.org/status/418" ; try other codes, for example:
;; "http://httpbin.org/status/200" ; success callback will be called.
;; "http://httpbin.org/status/400" ; you will see "Got 400."
:parser 'buffer-string
:success
(cl-function (lambda (&key data &allow-other-keys)
(when data
(with-current-buffer (get-buffer-create "*request demo*")
(erase-buffer)
(insert data)
(pop-to-buffer (current-buffer))))))
:error
(cl-function (lambda (&key error-thrown &allow-other-keys&rest _)
(message "Got error: %S" error-thrown)))
:complete (lambda (&rest _) (message "Finished!"))
:status-code '((400 . (lambda (&rest _) (message "Got 400.")))
(418 . (lambda (&rest _) (message "Got 418.")))))
(request
"https://github.com/tkf/emacs-request/commits/master.atom"
;; Parse XML in response body:
:parser (lambda () (libxml-parse-xml-region (point) (point-max)))
:success (cl-function
(lambda (&key data &allow-other-keys)
;; Just don't look at this function....
(let ((get (lambda (node &rest names)
(if names
(apply get
(first (xml-get-children
node (car names)))
(cdr names))
(first (xml-node-children node))))))
(message "Latest commit: %s (by %s)"
(funcall get data 'entry 'title)
(funcall get data 'entry 'author 'name))))))
Listing 13.7: Flexible PARSER option:
(request
"http://httpbin.org/put"
:type "PUT"
:data (json-encode '(("key" . "value") ("key2" . "value2")))
:headers '(("Content-Type" . "application/json"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'json data)))))
Listing 13.8: PUT JSON data
(request
"http://httpbin.org/put"
:type "PUT"
:data (json-encode '(("key" . "値1") ("key2" . "値2")))
:headers '(("Content-Type" . "application/json"))
:parser 'json-read
:encoding 'utf-8
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'json data)))))
Listing 13.9: PUT JSON data including non-ascii strings
;; (1) Prepend alist structure with a backtick (`) rather than single quote (')
;; to allow elisp evaluation of selected elements prefixed with a comma (,)
;; (2) This value is expected as a boolean so use the nil / t elisp alist denotation
;; (3) The function will be evaluated as it has been prefixed with a comma (,)
(request
"http://httpbin.org/put"
:type "PUT"
:data (json-encode `(("jsonArray" . (("item1" . "value 1") ;; (1)
("item2" . t) ;; (2)
("item3" . ,(your-custom-elisp-function)))))) ;; (3)
:headers '(("Content-Type" . "application/json"))
:parser 'json-read
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "I sent: %S" (assoc-default 'json data)))))
Listing 13.10: Another PUT JSON example (nested JSON using alist structure, how to represent a boolean & how to selectively evaluate lisp)
(request
"http:/hello.txt"
:unix-socket "/tmp/app.sock"
:parser (lambda () (buffer-string))
:success (cl-function
(lambda (&key data &allow-other-keys)
(message "Got: %s" data))))
Listing 13.11: GET with Unix domain socket data
Next: Configuration for request-el, Previous: Installation, Up: request [Index]