lisp - 获取 HTTP header 作为列表

标签 lisp common-lisp drakma

我想使用 drakma 获取 HTTP 请求的重定向 url。如果我传入“http://lisp.org/”,我想要返回“http://lisp.org/index.html”。查看文档,看起来我想要将 header 作为一个列表,drakma:http-request 应该给我一个列表。

The function returns SEVEN values - the body of the reply (but see below), the status code as an integer, an alist of the headers sent by the server where for each element the car (the name of the header) is a keyword and the cdr (the value of the header) is a string....

当我运行 drakma:http-request 时,如果我执行 (setf drakma:*header-stream* *standard-output*)。在以可用形式获取 header 时,我迷路了。

编辑:获取“http://lisp.org/”的重定向 URL:

(nth-value 3 (drakma:http-request "http://lisp.org/"))
Result: #<PURI:URI http://lisp.org/index.html>

最佳答案

在 Common Lisp 中返回多个值的函数需要特殊的语法来访问第一个值之外的值;这是一个方便的方法,一方面允许简单的函数调用简单地返回“最明显的东西”,另一方面也为需要它的调用者提供额外的信息。

HTTP-REQUEST 的情况下, header 列表是返回的第三个值,可以这样访问:

CL-USER> (nth-value 2 (drakma:http-request "http://lisp.org"))
((:DATE . "Tue, 26 Nov 2013 16:00:41 GMT") (:CONNECTION . "Close")
 (:SERVER . "AllegroServe/1.2.65") (:CONTENT-TYPE . "text/html")
 (:CONTENT-LENGTH . "459") (:LAST-MODIFIED . "Wed, 26 Oct 2011 02:26:26 GMT"))

有关 NTH-VALUE 和涉及处理多个值的其他构造的详细信息,请参阅 section 7.10.1 of Common Lisp, the Language .

关于lisp - 获取 HTTP header 作为列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20219289/

相关文章:

lisp - 返回与符号关联的所有值的函数

macros - 如何在普通 lisp 中编写宏定义宏

lisp - 什么情况需要用let代替let*?

ssl - CL+SSL SSL Error : Unsafe legacy renegotiation disabled. 如何绕过或解决?

common-lisp - PURI :URI how to get the actual string represented

lisp - LISP编程语言如何读取文件和保存数据

lisp - 普通口齿不清 : How to quote parenthese in SBCL

c - 使用 Embeddable Common Lisp 编译文件的正确方法是什么?

lisp - Common Lisp 中的 Uninterned 符号

asynchronous - drakma-async 和 cl-async 的不可预测行为