A higher-level alternative to using url-http-end-of-headers
, but equally undocumented, is url-insert-file-contents
:
(with-temp-buffer
(url-insert-file-contents
"https://api.stackexchange.com/2.2/questions/12464?site=emacs")
(json-parse-buffer :object-type 'alist))
or equivalently, prior to Emacs 27 compiled with native JSON support:
(require 'json)
(with-temp-buffer
(url-insert-file-contents
"https://api.stackexchange.com/2.2/questions/12464?site=emacs")
(let ((json-false :false))
(json-read)))
These result in:
((items .
[((tags .
["url" "http"])
(owner
(reputation . 10741)
(user_id . 227)
(user_type . "registered")
(accept_rate . 89)
(profile_image . "https://i.stack.imgur.com/ebO5J.jpg?s=128&g=1")
(display_name . "lunaryorn")
(link . "https://emacs.stackexchange.com/users/227/lunaryorn"))
(is_answered . t)
(view_count . 867)
(accepted_answer_id . 29798)
(answer_count . 3)
(score . 4)
(last_activity_date . 1517363132)
(creation_date . 1431861037)
(question_id . 12464)
(link . "https://emacs.stackexchange.com/questions/12464/go-to-body-after-url-retrieve-synchronously")
(title . "Go to body after url-retrieve-synchronously"))])
(has_more . :false)
(quota_max . 300)
(quota_remaining . 276))
The function url-insert-file-contents
wraps the better-documented functions url-insert-buffer-contents
and url-insert
and thus comes with the following goodies:
- Autoloaded by default.
- Exists since at least as far back as Emacs 21.
- Simple HTTP response error handling via
url-http-response-status
.
- Decodes data.
FWIW, it also comes with the guarantee of being used by lisp/emacs-lisp/package.el
.