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))))
Alan
  • 3
  • 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 '21 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 '21 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 ... https://github.com/tkf/emacs-request/blob/master/request.el#L439 – lawlist Jun 10 '21 at 21:33

1 Answers1

2

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.

John Kitchin
  • 11,891
  • 1
  • 20
  • 42