@kyanny's blog

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

Drakma で HTTP リクエストヘッダを送信する

(quicklisp:quickload :drakma)

(let ((drakma:*text-content-types* '(("application" . "json"))))
  (drakma:http-request "https://httpbin.org/get"
                       :user-agent "my user agent"
                       :additional-headers '(("X-Hello-World" . "Hello World"))))

いくつかのヘッダは直接 :user-agent のようにオプション引数?で指定できるが、 X- で始まる任意のヘッダなどを追加したい場合は :additional-headers を使う。

:additional-headers の値にはこういうリストを渡す。

'(
  ("Header Name 1" . "Header Value 1")
  ("Header Name 2" . "Header Value 2")
  )
CL-USER> (let ((drakma:*text-content-types* '(("application" . "json"))))
  (drakma:http-request "https://httpbin.org/get"
                       :user-agent "my user agent"
                       :additional-headers '(("X-Hello-World" . "Hello World"))))
"{
  \"args\": {}, 
  \"headers\": {
    \"Accept\": \"*/*\", 
    \"Connection\": \"close\", 
    \"Host\": \"httpbin.org\", 
    \"User-Agent\": \"my user agent\", 
    \"X-Hello-World\": \"Hello World\"
  }, 
  \"origin\": \"42.148.188.233\", 
  \"url\": \"https://httpbin.org/get\"
}
"
200
((:CONNECTION . "close") (:SERVER . "meinheld/0.6.1")
 (:DATE . "Thu, 17 Aug 2017 18:15:07 GMT") (:CONTENT-TYPE . "application/json")
 (:ACCESS-CONTROL-ALLOW-ORIGIN . "*")
 (:ACCESS-CONTROL-ALLOW-CREDENTIALS . "true") (:X-POWERED-BY . "Flask")
 (:X-PROCESSED-TIME . "0.00119519233704") (:CONTENT-LENGTH . "255")
 (:VIA . "1.1 vegur"))
#<PURI:URI https://httpbin.org/get>
#<FLEXI-STREAMS:FLEXI-IO-STREAM {10057DA113}>
T
"OK"