Onjsdev

Share


Axios Delete Request

Axios Delete Request


By onjsdev

Mar 28th, 2024

In Axios, you can easily make a DELETE request with the axios.delete method. This method takes the URL as its first argument and optional configuration options as the second argument.

Here's a basic example:

const axios = require('axios');

const apiUrl = 'https://example.com/api/resource/123';

axios.delete(apiUrl)
  .then(response => {
    console.log('Delete request successful', response);
  })
  .catch(error => {
    console.error('Error deleting resource', error);
  });

Here, the axios.delete method sends a DELETE request to the specified URL. After the server response to the request, you can use the then block to handle successful response, and the catch block to handle any errors occuring during the request process.

How To Send Headers With Axios Delete Request

You can also include additional configuration options, such as headers or request parameters, by passing an object to the axios.delete method.

Here is an example:

axios.delete(apiUrl, {
  headers: {
    'Authorization': 'Bearer your_access_token',
    'Content-Type': 'application/json',
  },
  params: {
    param1: 'value1',
    param2: 'value2',
  },
})
  .then(response => {
    console.log('Delete request successful', response);
  })
  .catch(error => {
    console.error('Error deleting resource', error);
  });

In this example, headers and parameters are included in the request. You can adjust the headers and parameters based on the requirements of the API you are interacting with.

Conclusion

With Axios, It is so easy to send anyt types of HTTP requests. In this article, we explored how to perform DELETE requests using axios.delete method.

If you want to read more about Axios, then you can check the following articles:

Thank you for reading.