【问题标题】:How to read POST data in the Guile web server如何在 Guile Web 服务器中读取 POST 数据
【发布时间】:2023-03-13 23:46:01
【问题描述】:

在 Guile 的网络服务器中,我似乎找不到任何有关读取 POST 数据的文档。它似乎与“请求”一起作为“正文”发送到我的入口点函数。看起来 body 被编码为字节向量,我可以将其解码为字符串:

(use-modules (rnrs bytevectors))
(utf8->string body)

所以我可以从这里开始解析字符串,但这似乎相当乏味且容易出错。有没有办法以某种列表的形式读取 POST 数据?

【问题讨论】:

  • 确实 POST 数据是主体,但它是使用 Guile 尚不支持的一些 RFC 编码的。
  • 您可以做些什么来解决这个问题,即根据您的表单发送一个 json,而不是执行原始 HTTP POST...
  • 感谢您的回复,鉴于该信息,我正在寻找替代品并找到“Artanis”,它是 Guile 的 GNU 网络服务器框架,它看起来支持 POST 并且更好地处理静态文件,这听起来更接近我的需求。所以我想我会调查一下。
  • 我没有找到如何在 artanis 中支持 POST 表单?您可以回复您的问题,以便我可以投票吗?发送!
  • 很抱歉,我最终也无法让它在 artanis 中工作。但它应该工作的方式在这里有几个例子:gnu.org/software/artanis/manual/manual.html 我仍然期待能够在未来用 guile 做到这一点,但我想它首先需要更多的开发(或者它可能已经在工作在阿塔尼斯,但我只是做得不对)

标签: guile


【解决方案1】:

这是decode 过程的代码,它将BODY 转换为列表的关联列表,其中键是表单字段的名称,值是与该键关联的值的列表。请注意,decode 返回的关联中与给定键关联的“值”始终是一个列表。

(define-module (web decode))

(use-modules (ice-9 match))
(use-modules (rnrs bytevectors))
(use-modules (srfi srfi-1))
(use-modules (srfi srfi-26))
(use-modules (web uri))

;;;
;;; decode
;;;

(define (acons-list k v alist)
  "Add V to K to alist as list"
  (let ((value (assoc-ref alist k)))
    (if value
        (let ((alist (alist-delete k alist)))
          (acons k (cons v value) alist))
        (acons k (list v) alist))))

(define (list->alist lst)
  "Build a alist of list based on a list of key and values.

   Multiple values can be associated with the same key"
  (let next ((lst lst)
             (out '()))
    (if (null? lst)
        out
        (next (cdr lst) (acons-list (caar lst) (cdar lst) out)))))

(define-public (decode bv)
  "Convert BV querystring or form data to an alist"
  (define string (utf8->string bv))
  (define pairs (map (cut string-split <> #\=)
                     ;; semi-colon and amp can be used as pair separator
                     (append-map (cut string-split <> #\;)
                                 (string-split string #\&))))
  (list->alist (map (match-lambda
                      ((key value)
                       (cons (uri-decode key) (uri-decode value)))) pairs)))

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多