Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
422在Clojure中发布文件上载时的不可处理实体响应_Clojure_Compojure_Clj Http - Fatal编程技术网

422在Clojure中发布文件上载时的不可处理实体响应

422在Clojure中发布文件上载时的不可处理实体响应,clojure,compojure,clj-http,Clojure,Compojure,Clj Http,我正在尝试模拟这个curl请求 curl "https://{subdomain}.zendesk.com/api/v2/uploads.json?filename=myfile.dat&token={optional_token}" \ -v -u {email_address}:{password} \ -H "Content-Type: application/binary" \ --data-binary @file.dat -X POST 使用以下代码 (PO

我正在尝试模拟这个curl请求

curl "https://{subdomain}.zendesk.com/api/v2/uploads.json?filename=myfile.dat&token={optional_token}" \
  -v -u {email_address}:{password} \
  -H "Content-Type: application/binary" \
  --data-binary @file.dat -X POST
使用以下代码

  (POST "/uploads" request
    (let [filename (get-in request [:params "file" :filename])
          file (get-in request [:params "file" :tempfile])
          url (str "https://REDACTED.zendesk.com/api/v2/uploads.json?filename=" filename)]
        (clj-http.client/post url {:headers {"Content-Type" “application/binary”}
                                   :multipart-params [{:name "file"
                                                       :content file
                                                       :mime-type "application/binary”}]})
但我从Zendesk收到了“422不可处理实体”的回复。根据请求,文件/tempfile将作为
#对象[java.io.file 0x3768306f”/var/folders/l3/7by17gp51sx2gb2ggykwl9zc0000gn/T/ring-multipart-6501654841068837352.tmp”
输入

我已经使用了clojure.java.io强制(如
clojure.java.io/output stream
),如中所述,但这没有帮助


(顺便说一句,我很确定我不需要进行身份验证,因为我可以通过邮递员直接上传到Zendesk。)

重新审视这一点后,解决方案很简单。Zendesk希望请求主体是二进制的(正如curl请求所示)。因此,在本例中,我将图像作为base64编码数据(与JSON一样)传递给服务器

然后,我使用该库将base64字符串转换为字节数组:

最后,您可以简单地将字节数组作为CLJHTTP库请求的主体传递给Zendesk

(client/post
  "https://REDACTED.zendesk.com/api/v2/uploads.jsonfilename=filename.jpg"
  {:headers {"Authorization" "Basic AUTHORIZATION_TOKEN"
             "Content-Type" "application/binary"}
   :body (byte-array-from-base64 base64-string)})
(client/post
  "https://REDACTED.zendesk.com/api/v2/uploads.jsonfilename=filename.jpg"
  {:headers {"Authorization" "Basic AUTHORIZATION_TOKEN"
             "Content-Type" "application/binary"}
   :body (byte-array-from-base64 base64-string)})