Documentation has examples for multipart requests but they don't seem to work
ashwinbhaskar opened this issue · 0 comments
ashwinbhaskar commented
clj-http
code
(let [url (-> (config/file-upload-url)
(str "?uploadType=multipart"))
parents (clojure.string/split folder-hierarchy #"/")
{:keys [status body] :as response} (http/post url {:headers {"Authorization" (str "Bearer " access-token)}
:multipart [{:name "metadata"
:content (-> {:name (str (utils/formatted-date-time)
"foo.apk")
:parents parents}
json/write-value-as-string)
:mime-type "application/json"
:encoding "UTF-8"}
{:name "file"
:content (clojure.java.io/file file-path)
:mime-type "application/vnd.android.package-archive"
:encoding "UTF-8"}]
:throw-exceptions false})]
(println response)
(condp = status
200 true
false))
The above code works.
Replacing http
with clk-http.lite.client
, I get the error
POST requests require a <code>Content-length</code> header. <ins>That's all we know.</ins>
So, I added content length header and calculated the content length.
(let [url (-> (config/file-upload-url)
(str "?uploadType=multipart"))
parents (clojure.string/split folder-hierarchy #"/")
multipart-content (-> [[{:name "metadata"
:content (-> {:name file-name
:parents parents}
json/write-value-as-string)
:mime-type "application/json"
:encoding "UTF-8"}]
[{:name "file"
:content (clojure.java.io/file file-path)
:mime-type "application/vnd.android.package-archive"
:encoding "UTF-8"}]]
utils/snake-caseize-and-stringify-keyword)
content-length (-> multipart-content
json/write-value-as-string
.getBytes
count)
{:keys [status body] :as response} (http/post url {:headers {"Authorization" (str "Bearer " access-token)
"Content-Type" "multipart/related"
"Content-Length" (str content-length)}
:multipart multipart-content
:content-type :json
:accept :json
:throw-exceptions false})]
(println response)
(condp = status
200 true
false))
But I still get the same error [Status code 411]. My guess is that the library does not send the content-length
header when the request is a multipart one