Making JSON requests within Emacs

后端 未结 3 1452
走了就别回头了
走了就别回头了 2020-12-28 17:40

I am in the early stages of writing an Emacs major mode for browsing and contributing to sites on the Stack Exchange network, in much the same way as dired and

3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-28 18:28

    The problem with other answers is that Stack Exchange API is GZIP'd and url.el shipped with Emacs does not automatically decompress it.

    Take a look at my request.el library which supports automatic decompression (to be honest, I just added the support). Here is an example to fetch the most active question in stackoverflow:

    (request
     "https://api.stackexchange.com/2.1/questions"
     :params '((order . "desc")
               (sort . "activity")
               (site . "stackoverflow"))
     :parser 'json-read
     :success (function*
               (lambda (&key data &allow-other-keys)
                 (let* ((item (elt (assoc-default 'items data) 0))
                        (title (assoc-default 'title item))
                        (tags (assoc-default 'tags item)))
                   (message "%s %S" title tags)))))
    

    request.el is well documented, comes with executable examples and is well tested.

提交回复
热议问题