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

any request returns "request-default-error-callback" #186

Closed dalanicolai closed 4 years ago

dalanicolai commented 4 years ago

Any request returns the "request-default-error-callback" error, although the same requests work with curl from the terminal.

For example trying curl "http://ip.jsontest.com/" in the terminal returns valid data. But trying (request "http://ip.jsontest.com/") returns:

#s(request-response nil nil nil nil nil "http://ip.jsontest.com/" nil (:error #[128 "\302\300\303\301\"\"\207" [request-default-error-callback ("http://ip.jsontest.com/") apply append] 6 "

(fn &rest ARGS2)"] :url "http://ip.jsontest.com/" :response #0 :encoding utf-8) #<buffer  *request curl*-256637> nil nil curl nil)

I am using emacs 26.3 with request version 20200517.1305. Of course I can provide more examples, but I assume this one should already work fine. I tried several things and searched for working examples but I can not find any.

dickmao commented 4 years ago

That particular url http://ip.jsontest.com is returning 503. For better or worse, request.el considers a status code greater than 400 to be an error, but command-line curl does not (command-line curl exits code zero). https://github.com/tkf/emacs-request/blob/912525c772984c6af0fd84acd6699ee43d91037a/request.el#L1209

dalanicolai commented 4 years ago

Sorry, I might have chosen the wrong example then. But trying e.g. (request "http://google.com") also returns the same kind of error message i.e.:

#s(request-response nil nil nil nil nil "http://google.com" nil (:error #[128 "\302\300\303\301\"\"\207" [request-default-error-callback ("http://google.com") apply append] 6 "

(fn &rest ARGS2)"] :url "http://google.com" :response #0 :encoding utf-8) #<buffer  *request curl*> nil nil curl nil)

Actually I was trying to do a POST using the gitlab markdown api which works fine using curl:

curl --header Content-Type:application/json --data '{"text":"Hello world! :tada:"}' "https://gitlab.com/api/v4/markdown"

But I can not get it to work with request:

(request
  "https://gitlab.com/api/v4/markdown"
  :type "POST"
  :data (json-encode '(("text" . "value")))
  :headers '(("Content-Type" . "application/json")))

it returns the message

#s(request-response nil nil nil nil nil "https://gitlab.com/api/v4/markdown" nil (:type "POST" :data "{\"text\":\"value\"}" :headers (("Content-Type" . "application/json")) :error #[128 "\302\300\303\301\"\"\207" [request-default-error-callback ("https://gitlab.com/api/v4/markdown") apply append] 6 "

(fn &rest ARGS2)"] :url "https://gitlab.com/api/v4/markdown" :response #0 :encoding utf-8) #<buffer  *request curl*> nil nil curl ("/tmp/emacs-requestHszhRu"))

I searched for any working example to find out what should be the behavior of request and tried to find what I did wrong, but I could not find any working example. So I tried the simplified request example above which returns the error already...

dickmao commented 4 years ago

Both of your examples (to google and gitlab) worked.

The "error" you're seeing is not an error but merely the ":error" callback member of the lisp object "request-response".

Perhaps the following will be less confusing:

(request-response-status-code (request "http://google.com/" :sync t))

dalanicolai commented 4 years ago

Thanks for your quick replies. Indeed I just found out I had to add the :sync t argument and how to use request-response-data subsequently. Unfortunately these things became not clear to me from the documentation, and I could not find any examples to find out how things should work. Clearly I am no expert, and I did not expect the call to be asynchronous by default (it is also not explicitly mentioned in the docs I think). Anyway, I will try to study more about cl and how to use its asynchronous requests incl. its callback functions. Anyway, thanks again for the reply incl. the hint...

On Mon, 6 Jul 2020 at 12:16, dickmao notifications@github.com wrote:

Both of your examples (to google and gitlab) worked.

The "error" you're seeing is not an error but merely the ":error" callback member of the lisp object "request-response".

Perhaps the following will be less confusing:

(request-response-status-code (request "http://google.com/" :sync t))

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/tkf/emacs-request/issues/186#issuecomment-654146098, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEMTOX3VCZX4WOJSLLZIS6TR2GQARANCNFSM4ORCJ7FA .