简体   繁体   中英

Get HTTP headers as alist

I want to get the redirect url of a HTTP-request using drakma. If I pass in " http://lisp.org/ ", I want " http://lisp.org/index.html " back. Looking at the docs, it looks like I want the headers as an alist and drakma:http-request should give me an alist.

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....

When I run drakma:http-request I get the message body and headers are shown if I do (setf drakma:*header-stream* *standard-output*) . I'm lost when it comes to getting the headers in usable form.

Edit: To get the redirect url of " http://lisp.org/ ":

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

Functions returning multiple values in Common Lisp require special syntax to access values beyond the first; this is a handy method of allowing a simple function call to trivially return "the most obvious thing", on the one hand, and also to provide additional information for the use of callers who have need of it, on the other.

The header alist, in the case of HTTP-REQUEST , is the third value returned, and can be accessed thus:

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"))

For details of NTH-VALUE and the other constructs involved in handling multiple values, see section 7.10.1 of Common Lisp, the Language .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM