Send body with axios delete request

Send body with axios delete request

Axios doesn't support delete requests with request body with the same signature as a post request would. But it is still possible without the need of the usage of the request function. The signature is just different.

// Separate config because axios doesn't support sending a request body on delete with the usual signature

const requestBody = {
  whatEver: 'to-send'
};

const config = TokenStorage.getAuthentication(); // AxiosRequestConfig
config.data = requestBody;

return axios.delete<void>(
  '/api/delete-whatever',
  config
)
  .then((response) => {
    // Show success
    return;
  })
  .catch((error) => {
    // Show error
    return;
  });
  
  ...
  
  public static getAuthentication(): AxiosRequestConfig {
    return {
      headers: { Authorization: `Bearer ${this.getToken()}` }
    };
  }

The AxiosRequestConfig contains a data property where you put the body of your request. This is of course an edge case. I've used it in a case where I needed to delete a user but had to define when all services for the settlement should be canceled.