我正在尝试使用 drakma 将一些 JSON 数据发布到 Web 服务.
(ql:quickload :st-json)
(ql:quickload :cl-json)
(ql:quickload :drakma)
(defvar *rc* (merge-pathnames (user-homedir-pathname) ".apirc"))
(defvar *user*
(with-open-file (s *rc*)
(st-json:read-json s)))
(defvar api-url (st-json:getjso "url" *user*))
(defvar api-key (st-json:getjso "key" *user*))
(defvar api-email (st-json:getjso "email" *user*))
(setf drakma:*header-stream* *standard-output*)
(defvar *req* '(("date" . "20071001") ("time" . "00") ("origin" . "all")))
(format t "json:~S~%" (json:encode-json-to-string *req*))
(defun retrieve (api request)
(let* ((cookie-jar (make-instance 'drakma:cookie-jar))
(extra-headers (list (cons "From" api-email)
(cons "X-API-KEY" api-key)))
(url (concatenate 'string api-url api "/requests"))
(stream (drakma:http-request url
:additional-headers extra-headers
:accept "application/json"
:method :post
:content-type "application/json"
:external-format-out :utf-8
:external-format-in :utf-8
:redirect 100
:cookie-jar cookie-jar
:content (json:encode-json-to-string request)
:want-stream t)))
(st-json:read-json stream)))
(retrieve "/datasets/tigge" *req*)
不幸的是,我得到了一个错误,尽管我认为数据似乎已正确编码为 JSON 并且 drakma 生成的 header 也是如此。显然 :content
有问题(错误消息中的整数列表只是 JSON 编码数据的 ASCII 代码列表)。
json:"{\"date\":\"20071001\",\"time\":\"00\",\"origin\":\"all\",\"type\":\"pf\",\"param\":\"tp\",\"area\":\"70\\/-130\\/30\\/-60\",\"grid\":\"2\\/2\",\"target\":\"data.grib\"}"
POST /v1/datasets/tigge/requests HTTP/1.1
Host: api.service.int
User-Agent: Drakma/1.3.0 (SBCL 1.1.5; Darwin; 12.2.0; http://weitz.de/drakma/)
Accept: application/json
Connection: close
From: me@gmail.com
X-API-KEY: 19a0edb6d8d8dda1e6a3b21223e4f86a
Content-Type: application/json
Content-Length: 193
debugger invoked on a SIMPLE-TYPE-ERROR:
The value of CL+SSL::THING is #(123 34 100 97 116 97 115 101 116 34 58 34
...), which is not of type (SIMPLE-ARRAY
(UNSIGNED-BYTE 8)
(*)).
知道这段代码有什么问题吗?非常感谢。
最佳答案
感谢 Drakma 和 Chunga drakma-devel 的一般兴趣列表中的 Kevin 和 Hans 帮助我解决问题 - 结果发现问题是由最新版本的 cl+ssl 中的错误引起的,该错误已在开发中修复分支。我用 quicklisp ,这是 Hans Hübner 建议我更新我的 cl+ssl 安装的建议,这很有效:
You can check out the latest cl+ssl - which contains a fix for the problem:
cd ~/quicklisp/local-projects/ git clone git://gitorious.org/cl-plus-ssl/cl-plus-ssl.git
Quicklisp will automatically find cl+ssl from that location. Remember to remove that checkout after you've upgraded to a newer quicklisp release that has the fix in the future.
关于json - 使用 drakma :http-request 发布 JSON 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15557383/