question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

RTK formData PUT not working

See original GitHub issue

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

image image

If I try to use the same endpoint with swagger it works image

I’m using “@reduxjs/toolkit”: “1.8.1”

Any ideas? Thank you in advance!

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:14 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
msutkowskicommented, Apr 27, 2022

Hmm. Actually, that may be part of the issue? I don’t think RTKQ is really meant for file uploads.

Also, I think it tries to send the body as JSON by default.

So, may be some combination of those two things - if you’re trying to send a file, you may have to alter the settings for the fetch send.

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

1reaction
ymoon715commented, Aug 30, 2022

yes @phryneas you are right, also fails with fetch so is not RTK issue. I’ll close this now and thank you guys for your help!

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Multipart/form-data" is not woking in React-redux ...
When I am sending data from postman form-data it is working fine but through frontend I am not getting that unexpected result.
Read more >
React + RTK Query + Material UI: Image Upload 2022
In this article, you'll learn how to upload images with React, RTK Query, Redux Toolkit, Zod, React Hook Form and Material UI.
Read more >
API call with multipart form-data not working : r/PowerShell
Hi all. I've attempted everything I could find online to convert this javascript to powershell and have been unsuccessful. 9/10 times I get...
Read more >
Redux Essentials, Part 8: RTK Query Advanced Patterns
RTK Query allows multiple components to subscribe to the same data, and will ensure that each unique set of data is only fetched...
Read more >
How to Multipart File Upload Using FormData with React ...
Use-cases include, but are not limited to admin panels, B2B applications and dashboards. Headless : Works with any UI framework. ⚙️ Zero- ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found