Skip to content

File uploads / content-type: multipart/form-data #25

Open
@knubie

Description

@knubie

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)}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions