redux-toolkit: RTK formData PUT not working
I have this file input
<input
type="file"
id="desktop-user-photo"
name="user-photo"
className="absolute inset-0 w-full h-full opacity-0 cursor-pointer border-gray-300 rounded-xs"
onChange={(e) => updatePicture(e.target.files ? e.target.files[0] : null)}
/>
that calls this method:
const [editPicture] = useEditPictureMutation();
const updatePicture = async (file: File | null) => {
const formData = new FormData();
formData.append('file', file);
await editPicture({ formData }).unwrap();
}
here the API slice
editPicture: builder.mutation<Profile, { formData: FormData }>({
query: ({ formData }) => ({
url: `/picture`,
method: 'PUT',
body: formData,
}),
invalidatesTags: [PROFILE_TAG],
}),
When I’m calling this endpoint I have and error from BE: [Nest] Unexpected end of multipart data


If I try to use the same endpoint with swagger it works
I’m using “@reduxjs/toolkit”: “1.8.1”
Any ideas? Thank you in advance!
About this issue
- Original URL
- State: closed
- Created 2 years ago
- Comments: 24 (4 by maintainers)
We don’t set headers when the body is
FormData
and file uploads should just work (I do this myself with RTKQ in many projects). You can see by the screenshots that it’s generating the form boundary and such as expected. This is most likely an API issue.Reference: https://github.com/reduxjs/redux-toolkit/blob/master/packages/toolkit/src/query/tests/fetchBaseQuery.test.tsx#L675-L694
This work
Coming here to say that, for me, I was also running into this issue and was driving me nuts, but it turned out to be because I was always setting the content-type to “application/json” within the
prepareHeaders
function, which overrides the default multipart-form headers.To get around this, I updated my
prepareHeaders
code as follows:Couldn’t figure out if there is a way to pass extra parameters from an individual endpoint (like somehow providing
ignoreDefaultHeaders: true
), so this is what I’m rolling with for now.Hope this helps someone else!
I was going crazy with wis issue and this is what worked for me:
First, I prepared the FormData like this (thanks @Zaidzularsya ):
Then I have my Api (secured) like this:
This worked for me. https://flaviocopes.com/fix-formdata-multipart-fetch/
Indeed @msutkowski your code looks very similar to mine, it should just work… I’ll check the API.
We are using nestjs and also from there looks so straight forward
I also tried different files, and from the swagger UI the upload works 😕
Thanks a lot!
When uploading formData with RTK, content-type should be undefined - just do not set it in the headers
https://github.com/reduxjs/redux-toolkit/issues/2287#issuecomment-1163565291 - 100% right and super helpful
(upd.) I left a comment here despite the fact that the issue is closed, since this thread is well indexed in the search engine and here is the most competent and clear information on the topic
@fs-innonova It is indeed an issue with fetch. You can use axios instead of baseQuery.
https://redux-toolkit.js.org/rtk-query/usage/customizing-queries#axios-basequery.
I used modified version of this example and got it to work.