How perform post call using react tool kit query without endpoint and operate on body

Viewed 15

Currently I am using rtk ( react tool kit) query to do an api call using redux , Current Url being static (ex:http://dummy.yt/fetch/) and body can be changed to get the desired result , API is an post request with a body looks like bellow

{
  "type": "Data_one",
  "include": "*",
  "limit":4,
  "offset": 0
}

Body updates here with every call for example offset will be changed to 1 , most of the examples on documentation talk about endpoints ( giving input to the URL) but I want this data to fetched In one single call and later optimize it using rtk query by alerting the body the way they are doing to the endpoint.

can we achieve this using RTK query?

1 Answers

You still need a query endpoint. Just one that does not return a string, but an object.

Instead of

query: arg => `some/url/${arg}`

do

query: arg => ({
  url: 'some/url',
  method: 'POST',
  body: {
    something: "foo",
    anotherThing: arg
  }
})
Related