Library

RTK-query로 게시판 작성 시, formdata를 넘기고 싶은 경우

반응형

https://stackoverflow.com/questions/76340703/send-form-data-with-rtk-query

 

send form data with rtk query

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) => { ...

stackoverflow.com

이걸 보고 시도했지만 에러가 났다. 왤까... 

rtk-query 홈페이지에서 formdata를 검색해도 나오는게 없어서 좀 헤맸다. 

  saveBoard: builder.mutation<boolean, { boardId: any; boardForm: FormData }>({
      query: (data) => {
        const { boardId, boardForm } = data
        return {
          url: boardId ? `/${boardId}` : '',
          method: boardId ? 'PATCH' : 'POST',
          body: boardForm,
          formData: true,
        }
      },
    }),

결론: headers 에 content-type:multipart/formdata를 넣을 필요가 없다. formData: true만 설정해도 된다. 

728x90
반응형