@kyanny's blog

My thoughts, my life. Views/opinions are my own.

Drakma で JSON API にアクセスする

(quicklisp:quickload :drakma)

(let ((drakma:*text-content-types* '(("application" . "json")))) ;; おまじない
  (drakma:http-request "https://httpbin.org/get"))

返り値は順に body status-code headers uri など(使いそうなのはこのへんまで)。

リファレンス http://weitz.de/drakma/#http-request

CL-USER> (let ((drakma:*text-content-types* '(("application" . "json"))))
  (drakma:http-request "https://httpbin.org/get"))
"{
  \"args\": {}, 
  \"headers\": {
    \"Accept\": \"*/*\", 
    \"Connection\": \"close\", 
    \"Host\": \"httpbin.org\", 
    \"User-Agent\": \"Drakma/2.0.3 (SBCL 1.3.20; Darwin; 16.7.0; http://weitz.de/drakma/)\"
  }, 
  \"origin\": \"42.148.188.233\", 
  \"url\": \"https://httpbin.org/get\"
}
"
200
((:CONNECTION . "close") (:SERVER . "meinheld/0.6.1")
 (:DATE . "Thu, 17 Aug 2017 18:10:23 GMT") (:CONTENT-TYPE . "application/json")
 (:ACCESS-CONTROL-ALLOW-ORIGIN . "*")
 (:ACCESS-CONTROL-ALLOW-CREDENTIALS . "true") (:X-POWERED-BY . "Flask")
 (:X-PROCESSED-TIME . "0.00083589553833") (:CONTENT-LENGTH . "272")
 (:VIA . "1.1 vegur"))
#<PURI:URI https://httpbin.org/get>
#<FLEXI-STREAMS:FLEXI-IO-STREAM {100575E1A3}>
T
"OK"