tkf / emacs-request

Request.el -- Easy HTTP request for Emacs Lisp
http://tkf.github.com/emacs-request/
GNU General Public License v3.0
629 stars 93 forks source link

Pass parameters to callbacks #137

Closed karthic891 closed 5 years ago

karthic891 commented 5 years ago

Is there any way to pass parameters to the callbacks that we pass to 'success', 'error', 'status-code'?

For instance, is there a way to pass extra-param to the success' callback?

(defun get-something (extra-param)
  (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 -- %s" (assoc-default 'args data) extra-param)))))
dickmao commented 5 years ago
(require 'request)
(require 'json)
(defun get-something (key value extra-param)
  (request
   "http://httpbin.org/get"
   :parser 'json-read
   :params `((,key . ,value))
   :success (apply-partially
             (cl-function
              (lambda (extra-param &key data &allow-other-keys)
                (message "Got %s with callback parameterized by %s"
                         (alist-get 'args data) extra-param)))
             extra-param)))
(get-something "e" "mc^2" "extra")
karthic891 commented 5 years ago

Perfect! Thank you.