kenny229
kenny229

Reputation: 177

How to pass an array of id's to delete using an endpoint rather than just one Id

I am trying to remove an array of id's that has to be removed in the backend. I am going to use an endpoint that will take this and execute the delete request with redux. It will be at the endpoint:

DELETE /api/v1/algo/configs?configIds=1,2,3 

The 1, 2 , and 3 just represents dummy id's. Which are passed as query parameter and the response would look something like this:

{
    "configIds": ["1","2","3"]
}

These above is what should be returned(the deleted array)

Here is what I have so far on the frontend, which is a handler that should asyncronously remove the array one by one so that rate limit is not going crazy.

async function deleteAll() {
        for (const id in filteredStoppedConfigs) {
          await actions.clearAllConfigs(filteredStoppedConfigs[id]);
        }
      }

filteredStoppedConfigs is an array of id numbers to remove

And here is an endpoint I have already implemented to delete one id. It works and does the job. But not sure how to implement it at the new endpoint which deletes a whole array. Essentially clearing the whole array

async function deleteConfig(configId) {
  const options = {
    method: 'DELETE',
    headers: {
      'content-type': 'application/json'
    },
    url: `${ALGOS_API_ROOT}/configs/${configId}`
  };
  return axios(options);
}

Been trying to research online but no one really asked a similar question. Any help is appreciated. Thanks!

Upvotes: 1

Views: 4450

Answers (2)

Damilare Olusakin
Damilare Olusakin

Reputation: 439

OPTION 1 You will need to parameterize your array elements, creating a string that looks like DELETE /api/v1/algo/configs?configIds[]=1&configIds[]=2&configIds[]=3

OPTION 2 Use a POST or PATCH ROUTE and pass the JSON string of the array in the body of the request. POST|PATCH /api/v1/algo/configs

{
    headers: ....,
    ....
    body: {
      ids: stringifiedArray
    },
    ....
}

OPTION 3 NOT SUPER ENCOURAGED A simple endpoint like so is fine: DELETE /api/v1/algo/configs.

Next, you can probably stringify your array JSON.stringify(arrayHere) and pass that stringified array as part of the body of the request like so:

body: {
  ids: stringifiedArray
}

You will then receive this body parameter on your backend(whitelisting it), parse it and then used it for your desired operation

Upvotes: 1

Wazeed
Wazeed

Reputation: 1280

You can update deleteConfig as below to handle a request to delete for one config id as well as multiple config ids

async function deleteConfig(configId) {
    const options = {
        method: 'DELETE',
        headers: {
            'content-type': 'application/json'
        },
        url: configId.includes(",") ? `${ALGOS_API_ROOT}/configs?configIds=${configId}` : `${ALGOS_API_ROOT}/configs/${configId}`
    };
    return axios(options);
}

To handle a request to delete for one config id

deleteConfig(single_config_id);

To handle a request to delete for multiple config ids

deleteConfig(array_of_config_ids.join());

Upvotes: 0

Related Questions