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

confusing output #192

Closed zbelial closed 4 years ago

zbelial commented 4 years ago

When I execute the example

(request
 "http://httpbin.org/post"
 :type "POST"
 :data '(("key" . "value") ("key2" . "value2"))
 ;; :data "key=value&key2=value2"  ; this is equivalent
 :sync t
 :parser 'json-read
 :success (cl-function
           (lambda (&key data &allow-other-keys)
             (message "I sent: %S" (assoc-default 'form data)))))

it outputs

I sent: ((key . "value") (key2 . "value2"))
#s(request-response 200 nil ((args) (data . "") (files) (form (key . "value") (key2 . "value2")) (headers (Accept . "*/*") (Accept-Encoding . "deflate, gzip, zstd") (Content-Length . "21") (Content-Type . "application/x-www-form-urlencoded") (Host . "httpbin.org") (User-Agent . "curl/7.72.0") (X-Amzn-Trace-Id . "Root=1-5fa01f2b-7198ad173422ae91290118d3")) (json) (origin . "123.119.237.133") (url . "http://httpbin.org/post")) nil success "http://httpbin.org/post" t (:type "POST" :data "key=value&key2=value2" :sync t :parser json-read :success (closure (t) (&rest --cl-rest--) "

(fn &key DATA &allow-other-keys)" (let* ((data (car (cdr (plist-member --cl-rest-- ':data))))) (message "I sent: %S" (assoc-default 'form data)))) :error #[128 "\"\"" [request-default-error-callback ("http://httpbin.org/post") apply append] 6 "

(fn &rest ARGS2)"] :url "http://httpbin.org/post" :response #0 :encoding utf-8) #<killed buffer> "HTTP/1.1 200 OK
Date: Mon, 02 Nov 2020 15:00:59 GMT
Content-Type: application/json
Content-Length: 499
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
" nil curl ("/tmp/emacs-requestxxRNNA"))

I fail to figure out why by default it outputs those following "I sent..." and how to disable it. It is really wired IMHO.

dickmao commented 4 years ago

Perhaps you will like this better:

(prog1 nil
  (request
    "http://httpbin.org/post"
    :type "POST"
    :data '(("key" . "value") ("key2" . "value2"))
    :sync t
    :parser 'json-read
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (message "I sent: %S" (assoc-default 'form data))))))

[edit] or better yet,

(let (result)
  (request
    "http://httpbin.org/post"
    :type "POST"
    :data '(("key" . "value") ("key2" . "value2"))
    :sync t
    :parser 'json-read
    :success (cl-function
              (lambda (&key data &allow-other-keys)
                (setq result (format "I sent: %S" (assoc-default 'form data))))))
  result)

You need to make a distinction between "stdout" and "return value". Alas, both get issued to the *Messages* buffer.

zbelial commented 4 years ago

Thank you for your reply, and sorry for that I didn't make it clear that the 'output' was actually from *Message*.

But I have a little advice that request's document is not very clear as it does not mention it will return something. I know that it mentions Response object, it's not clear enough IMO.

Thanks again.