@kyanny's blog

My life. Opinions are my own.

drakma:http-request の返り値を変数に束縛する

(quicklisp:quickload :drakma)

(let* ((drakma:*text-content-types* '(("application" . "json")))
       (token "helloworld")
       (authorization (concatenate 'string "token" " " token)))
  (multiple-value-bind (body status headers)
      (drakma:http-request "https://httpbin.org/get"
                           :user-agent "my user agent"
                           :additional-headers `(("Authorization" . ,authorization)))
    (print body)
    (print "----")
    (print status)
    (print "----")
    (print headers)
    nil))

drakma:http-request は多値を返すので multiple-value-bind を使って変数束縛する。

リファレンス http://www.lispworks.com/documentation/HyperSpec/Body/m_multip.htm

CL-USER> (let* ((drakma:*text-content-types* '(("application" . "json")))
       (token "helloworld")
       (authorization (concatenate 'string "token" " " token)))
  (multiple-value-bind (body status headers)
      (drakma:http-request "https://httpbin.org/get"
                           :user-agent "my user agent"
                           :additional-headers `(("Authorization" . ,authorization)))
    (print body)
    (print "----")
    (print status)
    (print "----")
    (print headers)
    nil))

"{
  \"args\": {}, 
  \"headers\": {
    \"Accept\": \"*/*\", 
    \"Authorization\": \"token helloworld\", 
    \"Connection\": \"close\", 
    \"Host\": \"httpbin.org\", 
    \"User-Agent\": \"my user agent\"
  }, 
  \"origin\": \"42.148.188.233\", 
  \"url\": \"https://httpbin.org/get\"
}
" 
"----" 
200 
"----" 
((:CONNECTION . "close") (:SERVER . "meinheld/0.6.1")
 (:DATE . "Thu, 17 Aug 2017 18:26:19 GMT") (:CONTENT-TYPE . "application/json")
 (:ACCESS-CONTROL-ALLOW-ORIGIN . "*")
 (:ACCESS-CONTROL-ALLOW-CREDENTIALS . "true") (:X-POWERED-BY . "Flask")
 (:X-PROCESSED-TIME . "0.00106692314148") (:CONTENT-LENGTH . "260")
 (:VIA . "1.1 vegur")) 
NIL