如何将对Blitline JSON服务的cURL调用转换为clojure?
Blitline提供了从命令行调用的示例;这将如何翻译成clojure如何将对Blitline JSON服务的cURL调用转换为clojure?,clojure,Clojure,Blitline提供了从命令行调用的示例;这将如何翻译成clojure $ curl "http://api.blitline.com/job" -d json='{ "src" : "http://www.google.com/logos/2011/yokoyama11-hp.jpg", "functions" : [ {"name": "blur", "params" : {"radius" : 0.0, "sigma" : 2.0}, "save" : { "image_identifie
$ curl "http://api.blitline.com/job" -d json='{ "src" : "http://www.google.com/logos/2011/yokoyama11-hp.jpg", "functions" : [ {"name": "blur", "params" : {"radius" : 0.0, "sigma" : 2.0}, "save" : { "image_identifier" : "some_id" }} ]}'
您可以使用clj-http.client和clojure.data.json。以下是我用来与城市飞艇JSON API对话的一些代码,作为示例:
(ns my-ns
(:require [clj-http.client :as http]
[clojure.data.json :as json]))
(def uu-base-url
"https://go.urbanairship.com")
(def auth ["secret" "password"])
(defn broadcast-message*
[auth text]
(http/post (str uu-base-url "/api/push/broadcast/") ;; target url
{:basic-auth auth ;; leave this out if you don't need HTTP basic authentication
:content-type "application/json"
:body (json/json-str
;; clojure data to be converted into JSON request body
{:aps {:badge 1
:alert text}})}))
您可以使用clj-http.client和clojure.data.json。以下是我用来与城市飞艇JSON API对话的一些代码,作为示例:
(ns my-ns
(:require [clj-http.client :as http]
[clojure.data.json :as json]))
(def uu-base-url
"https://go.urbanairship.com")
(def auth ["secret" "password"])
(defn broadcast-message*
[auth text]
(http/post (str uu-base-url "/api/push/broadcast/") ;; target url
{:basic-auth auth ;; leave this out if you don't need HTTP basic authentication
:content-type "application/json"
:body (json/json-str
;; clojure data to be converted into JSON request body
{:aps {:badge 1
:alert text}})}))
以上答案是正确的。为完整起见,我附上您需要的完整代码:
(require '[clj-http.client :as http])
(require '[clojure.data.json :as json])
(def post
(http/post "http://api.blitline.com/job" {
:body
(json/json-str
{ "json"
{ "application_id" "sgOob0A3b3RdYaqwTEJCpA"
"src" "http://www.google.com/logos/2011/yokoyama11-hp.jpg"
"functions" [ {
"name" "blur"
"params" {
"radius" 0.0
"sigma" 2.0
}
"save" { "image_identifier" "some_id" }
}
]}})
:body-encoding "UTF-8"
:content-type :json
:accept :json
}))
(json/read-json (:body post))
以上答案是正确的。为完整起见,我附上您需要的完整代码:
(require '[clj-http.client :as http])
(require '[clojure.data.json :as json])
(def post
(http/post "http://api.blitline.com/job" {
:body
(json/json-str
{ "json"
{ "application_id" "sgOob0A3b3RdYaqwTEJCpA"
"src" "http://www.google.com/logos/2011/yokoyama11-hp.jpg"
"functions" [ {
"name" "blur"
"params" {
"radius" 0.0
"sigma" 2.0
}
"save" { "image_identifier" "some_id" }
}
]}})
:body-encoding "UTF-8"
:content-type :json
:accept :json
}))
(json/read-json (:body post))
谢谢,我没有得到的是用
“json”{…}
包装所有东西。我没有意识到cURL-djson='{…}'
意味着json
本身实际上是散列的一部分。谢谢,我没有得到的是在“json”{…}
中包装所有内容。我没有意识到cURL-djson='{…}'
意味着json
本身实际上是散列的一部分。