lambdaisland / lambdaisland/fetch

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

Open
#25 2 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Clojure
Stars
132
Forks
12
PR merge metrics
No merged PRs in 30d

Description

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):

https://github.com/lambdaisland/fetch/blob/1fe9fab75b47f5a245dfa105a87ffe8da539118d/src/lambdaisland/fetch.cljs#L102-L104

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.

https://github.com/lambdaisland/fetch/blob/1fe9fab75b47f5a245dfa105a87ffe8da539118d/src/lambdaisland/fetch.cljs#L80-L81

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read src/lambdaisland/fetch.cljs at the referenced content-type and body-encoding lines, then trace the js/fetch entry point. Confirm how a raw FormData body can pass through without an explicit Content-Type header. Done means multipart/form-data uploads work with FormData while existing encoded body options remain unchanged.

Written by the indexing model from the issue text.

Assessment

Tech stack
clojure, javascript
Domain
api, web-dev
Issue type
Feature
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.