0

I know how to use request.el to get data and insert it into a buffer, but what if I want to simply have request.el return the data itself (e.g., for saving in a variable)?

Here's my attempt, but this doesn't actually return data (in this case a small amount of test JSON):

(request "https://jsonplaceholder.typicode.com/todos/1"
:parser 'json-read
:success (cl-function
      (lambda (&key data &allow-other-keys)
    data))))
3
  • How about something like this?: (let* ((buf (url-retrieve-synchronously "https://api.github.com/users/lawlist/gists")) (str (with-current-buffer buf (buffer-string)))) (kill-buffer buf) str)
    – lawlist
    Jun 10, 2021 at 17:47
  • That would absolutely work, but I'm hoping to use request.el for this because it can create more complex url requests.
    – Alan
    Jun 10, 2021 at 21:09
  • The doc-string for parser states in part: "So, for example, you can pass json-read to parse JSON object in the buffer. To fetch whole response as a string, pass buffer-string." Have a look at the doc-string for other clues and perhaps try using buffer-string as suggested ... I don't have that particular library installed at the moment, and merely git cloned it to see what makes it tick ... github.com/tkf/emacs-request/blob/master/request.el#L439
    – lawlist
    Jun 10, 2021 at 21:33

1 Answer 1

1

You are looking for this I think:

(request-response-data
 (request "https://jsonplaceholder.typicode.com/todos/1"
   :parser 'json-read
   :sync t))

the :sync arg avoids the need for a callback I think by making it wait for the request to be done.

1
  • Perfect---thank you!
    – Alan
    Jun 12, 2021 at 2:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.