At the moment it doesn't seem possible to send files as multipart/form-data as described here: Using the Fetch API - Web APIs | MDN.
For example with js/fetch we could do something like this:
(-> (js/fetch "https://example.com/profile/avatar"
#js{:method "POST"
:body (doto (js/FormData.) (.append "file" blob file-name))})
and js/fetch will automatically set the Content-Type header to multipart/form-data and encode the body properly.
However in lambdaisland/fetch the body is always encoded according to the :content-type option before it's passed to js/fetch (unless it's a string):
|
(j/assoc! :body (if (string? body) |
|
body |
|
(encode-body content-type body opts))))] |
There isn't really a way to leave the body decoded without passing a string.
I think the simplest workaround would probably be to add a new :raw or nil option for :content-type that just passes the :body through to js/fetch decoded.
It's possible to bypass the encoding today by using :default as the :content-type, but that will also explicitly set the Content-Type header to nil, since it's not in the content-types map and is really just a hack for the encoding multimethod.
|
(let [fetch-headers #js {"Accept" (c/get content-types accept) |
|
"Content-Type" (c/get content-types content-type)}] |
At the moment it doesn't seem possible to send files as
multipart/form-dataas described here: Using the Fetch API - Web APIs | MDN.For example with
js/fetchwe could do something like this:and
js/fetchwill automatically set theContent-Typeheader tomultipart/form-dataand encode the body properly.However in lambdaisland/fetch the body is always encoded according to the
:content-typeoption before it's passed tojs/fetch(unless it's a string):fetch/src/lambdaisland/fetch.cljs
Lines 102 to 104 in 1fe9fab
There isn't really a way to leave the body decoded without passing a string.
I think the simplest workaround would probably be to add a new
:raworniloption for:content-typethat just passes the:bodythrough tojs/fetchdecoded.It's possible to bypass the encoding today by using
:defaultas the:content-type, but that will also explicitly set theContent-Typeheader tonil, since it's not in thecontent-typesmap and is really just a hack for the encoding multimethod.fetch/src/lambdaisland/fetch.cljs
Lines 80 to 81 in 1fe9fab