使用 emacs-web-server 解析 webhook

Parsing webhook using emacs-web-server

我正在使用 emacs-web-server and ngrok 从 GitHub 接收 Webhook。关于设置,我通过 Github 网站上的 Redeliver 按钮手动触发来自 Github 的请求:

我正在使用以下代码来处理来自 GitHub 的请求:

(ws-start
 '(((:POST . ".*") .
    (lambda (request)
      (with-slots (process headers pending) request
        (print "# pending start #")
        (print pending)))))
 9005)

示例 emacs-web-server POST 可在此处找到: http://eschulte.github.io/emacs-web-server/POST-Echo.html#POST-Echo

一切正常,printing of pending 在我的 Messages 缓冲区中显示以下内容:

但是我在将有效负载隔离到一个变量中时遇到了问题(也就是说,双行后的所有内容都断开了:{\"ref\".. 等等)。我尝试了几个正则表达式;当我在带有待定变量的粘贴副本的临时缓冲区中使用它们时,这些似乎都有效,但在实际有效负载场景中不起作用。大概 ^Ms 与此有关,因为当内容被粘贴到另一个缓冲区时它们不存在。如果有人能指出我正确的方向,那将不胜感激。

谢谢!

在您的示例中,您有一个包含 HTTP 请求的字符串,其中正文是 JSON 文档。您绝对不想自己用正则表达式解析它。

这种情况下的常见过程是先使用合适的库解析 HTTP 请求,从结果中获取正文(使用库的方法调用),最后解析 JSON 文档在正文中使用另一个合适的库。

例如,我在 Clojure 中完成了此操作,用于接收来自 GitHub 的 webhook 调用,不过我从未将 Emacs 用于此类操作。

对 HTTP 请求解析器和 JSON 解析器的快速搜索产生了 https://tkf.github.io/emacs-request/ and http://tess.oconnor.cx/2006/03/json.el。也许它们对你有用。

receiver.el is an Emacs library I wrote to accomplish what I believe you're looking to do, but using elnode as the web server and beame-insta-ssl 作为隧道。将它修改为使用 ngrok 而不是 beame-insta-ssl 很容易,并且欢迎提出这种效果的拉取请求。

转回 @Rick77 最近询问。

我能够通过手动读取响应来让请求 handling/parsing 工作。

然而最近我遇到了类似的情况(传出请求除外)并且发现使用'request 和'json 库是一种更简单的方法:

(require 'json)
(require 'request)

(defun get-response-data (url)
  (let* ((res (request url :sync t))
         (data (request-response-data res)))
    (json-read-from-string data)))

https://github.com/ryancrum/json.el

https://tkf.github.io/emacs-request/