Search code examples
reactjsreduxredux-toolkit

Reduxjs endpoint doesn't send payload


I have this in React:

 updateData: builder.mutation({
      query: ({ myParams }) => ({
        url: `/update-data`,
        method: 'POST',
        dataType: 'json',
        myParams, // doc says this is the way to go
        data: myParams,
        body: myParams,
        params: myParams // I have tried with these 4
      }),
    }),

called by:

updateData({
      myParams,
    })
      .unwrap()
      .then((data: any) => {
         console.log('DATA: ', data);
      })
      .catch(({ error }) => {
         console.log('DATA ERROR: ', error);
      });

This calls the endpoint but I get a 400 saying I'm missing a param. When checking the network tab I don't see any payload at all.

If I run this in the browser I get a 200 and I can check the payload:

$.ajax({
    url: `/update-data`,
    type: 'POST',
    data: myParams,
    success: function (data) {
      console.log('DATA: ', data);
    }
  });

More info: https://redux-toolkit.js.org/rtk-query/api/createApi#query


Solution

  • updateData: builder.mutation({
         query: ( myParams ) => ({
            url: `/update-data`,
            method: 'POST',
            dataType: 'json',
            body: new URLSearchParams(myParams),
          }),
    }),