Blog.

How to create a DELETE request with axios

Cover Image for How to create a DELETE request with axios

The easiest way to create a DELETE request with Axios is by using axios.delete method. This method accepts two parameters.

 

The easiest way to create a DELETE request with Axios is by using axios.delete method. This method accepts two parameters. The first parameter is the URL (type string) and the second is the body of the request (type object or type string).

const url = 'https://jsonplaceholder.typicode.com/posts/1'
const res = await axios.delete(url)

Send data with axios.delete()

const url = 'https://jsonplaceholder.typicode.com/posts/1'
const user = {
  id: 1,
  name: 'John Doe',
}

const res = await axios.delete(url, {
  data: user,
})

Send data with axios config

const url = 'https://jsonplaceholder.typicode.com/posts/1'
const config = {
  url,
  method: 'DELETE',
  data: {
    name: 'John Doe',
  },
}

const res = await axios(config)

According to axios's documentation, data property represents the data to be sent as the request body and is only applicable for request methods PUT, POST, DELETE and PATCH

Form-Encoded Request Bodies

To send data in Axios using the application/x-www-form-urlencoded you need to send the data as strings and for that you can use qs library like so:

const qs = require('qs')
const res = await axios.delete(url, qs.stringify({ name: 'John Doe' }))

Daniel Turuș

@danielturus

Hi! My name is Daniel and I am a Full-stack JavaScript developer.

If you like my material, please consider following me on Twitter to get notified when new posts are published, ask me a question and stay in touch.