send form data with rtk query
Asked Answered
P

5

6

I wanna send an image with form data to the nest js server with RTK query but the file didn't send. the code are:

    uploadImage: builder.mutation<any, any>({
  query: (imageFile) => {
    var bodyFormData = new FormData();
    bodyFormData.append('file', imageFile);
    console.log({ bodyFormData, imageFile });
    return {
      url: '/uploader/image',
      method: 'POST',
      headers: {
        'Content-Type': 'multipart/form-data;'
      },
      body: { bodyFormData }
    };
  }
})

I can send an image with Axios but in RTK I cant.

Puke answered 26/5, 2023 at 12:12 Comment(0)
T
8

You should also pass another parameter called formData in RTK Query while passing formdata in API request.

  uploadImage: builder.mutation<any, any>({
  query: (imageFile) => {
    var bodyFormData = new FormData();
    bodyFormData.append('file', imageFile);
    console.log({ bodyFormData, imageFile });
    return {
      url: '/uploader/image',
      method: 'POST',
      headers: {
        'Content-Type': 'multipart/form-data;'
      },
      body: { bodyFormData },
      formData:true           //add this line πŸ‘ˆ
    };
  }
})
Tobin answered 26/5, 2023 at 18:58 Comment(3)
Cannot read properties of undefined (reading 'filename') still get error – Puke
Content-Type should be omitted from headers because it's added automatically with the correct boundary if formData is true. – Electrojet
That prop formData: true doesn't exist anymore. Adding headers: { 'Content-Type': 'multipart/form-data' } is enough. – Vallombrosa
V
2

Remove body: { bodyFormData } instead use body: bodyFormData.

Additionally, you could try using this structure:

var bodyFormData = new FormData();
    bodyFormData.append('file', {
      uri: imageFileUri, 
      type: "image/jpeg",
      name: imageFileUri?.split('/')[imageFileUri?.split('/').length - 1]
    });
Vallombrosa answered 1/9, 2023 at 2:43 Comment(0)
E
2

Just sending the FormData directly on the body works for me. No need to manually set the header (it will be set for you) nor adding formData: true.

Example:

    uploadImage: builder.mutation<any, any>({
      query: (imageFile) => {
        var bodyFormData = new FormData();
        bodyFormData.append('file', imageFile);
        console.log({ bodyFormData, imageFile });
        return {
          url: '/uploader/image',
          method: 'POST',
          body: bodyFormData
        };
      }
    })
Erode answered 1/3 at 12:51 Comment(0)
E
0

Adding header 'Skip-Content-Type': true, helped in my case

Example:

uploadImage: builder.mutation<any, any>({
  query: (imageFile) => {
    var bodyFormData = new FormData();
    bodyFormData.append('file', imageFile);
    console.log({ bodyFormData, imageFile });
    return {
      url: '/uploader/image',
      headers: {
        'Skip-Content-Type': true
      },
      method: 'POST',
      body: bodyFormData
    };
  }
})
Elicia answered 9/10 at 16:16 Comment(0)
S
-3

In my case, adding "Accept", "application/json" in addition to formData: true solved the problem.

Syllabism answered 28/9, 2023 at 10:2 Comment(1)
formData: true Doesn't exist anymore. – Vallombrosa

© 2022 - 2024 β€” McMap. All rights reserved.