【问题标题】:Access the querystring in a Spiffy app在 Spiffy 应用程序中访问查询字符串
【发布时间】:2023-03-25 08:07:01
【问题描述】:

人们不应该在这里问这个问题,但是由于糟糕的文档,我如何访问 Spiffy (egg) 应用程序中的查询字符串?谢谢!

(use intarweb spiffy sxml-serializer) 

(tcp-buffer-size 2048)
(server-port 80)

(handle-not-found (lambda (path)    
    ; (print (request-uri (current-request)))
    ; (print (request-method (current-request)))
    ; (print (current-pathinfo (current-request)))
    ; (print (current-file))
    ; (print (remote-address))
    (send-response 
        body: (serialize-sxml
            `(div (@ (class "page"))
                (h1 ,path))
            method: 'html))))

(start-server)

【问题讨论】:

    标签: scheme chicken-scheme spiffy


    【解决方案1】:

    我不确定您为什么要访问查询字符串,但是 Spiffy 中的 URI 对象来自请求对象,正如您正确的那样 确定。请求对象来自 intarweb,它粘贴了一个 request-uri 属性中的 uri-common 对象。

    您可以使用 uri-common 访问组成部分 访问器,如 URI-common egg reference

    中所述

    为方便起见,查询字符串被解析为 alist,并且 可通过(uri-query (request-uri (current-request)))访问。

    “原始”查询字符串的访问方式稍有不同: uri-common egg 是低级 egg 的方便包装器 uri-generic,您可以通过调用(uri->uri-generic URI) 访问它, 其中 URI 是 (request-uri (current-request)),和以前一样。

    然后,您可以通过uri-query 过程访问查询字符串 从那个蛋。这是一个可以在 REPL 上使用的简单示例:

    #;1> (use uri-common (prefix uri-generic generic:))
    #;2> (uri-reference "http://foo?x=y")
    #<URI-common: scheme=http port=#f host="foo" path=() query=((x . "y")) fragment=#f>
    #;3> (uri-query #2)
    ((x . "y"))
    #;4> (uri->uri-generic #2)
    #<<URI>>
    #;5> (generic:uri-query #4)
    "x=y"
    

    我在这里所做的是在 uri-generic 中的所有程序前面加上 “通用的:”。这是必要的,因为 uri-common “阴影”所有 由 uri-generic 定义的过程。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-16
      • 1970-01-01
      • 1970-01-01
      • 2018-03-05
      • 1970-01-01
      • 2010-09-11
      • 1970-01-01
      • 2013-07-06
      相关资源
      最近更新 更多