javascript - Axios Delete request with body and headers?

ID : 20327

viewed : 24

Tags : javascriptreactjshttpaxioshttp-deletejavascript

Top 5 Answer for javascript - Axios Delete request with body and headers?

vote vote

93

So after a number of tries, I found it working.

Please follow the order sequence it's very important else it won't work

axios.delete(URL, {   headers: {     Authorization: authorizationToken   },   data: {     source: source   } }); 
vote vote

89

axios.delete does support a request body. It accepts two parameters: url and optional config. You can use config.data to set the request body and headers as follows:

axios.delete(url, { data: { foo: "bar" }, headers: { "Authorization": "***" } }); 

See here - https://github.com/axios/axios/issues/897

vote vote

70

Here is a brief summary of the formats required to send various http verbs with axios:

  • GET: Two ways

    • First method

      axios.get('/user?ID=12345')   .then(function (response) {     // Do something   }) 
    • Second method

      axios.get('/user', {     params: {       ID: 12345     }   })   .then(function (response) {     // Do something   }) 

    The two above are equivalent. Observe the params keyword in the second method.

  • POST and PATCH

    axios.post('any-url', payload).then(   // payload is the body of the request   // Do something )  axios.patch('any-url', payload).then(   // payload is the body of the request   // Do something ) 
  • DELETE

    axios.delete('url', { data: payload }).then(   // Observe the data keyword this time. Very important   // payload is the request body   // Do something ) 

Key take aways

  • get requests optionally need a params key to properly set query parameters
  • delete requests with a body need it to be set under a data key
vote vote

66

axios.delete is passed a url and an optional configuration.

axios.delete(url[, config])

The fields available to the configuration can include the headers.

This makes it so that the API call can be written as:

const headers = {   'Authorization': 'Bearer paperboy' } const data = {   foo: 'bar' }  axios.delete('https://foo.svc/resource', {headers, data}) 
vote vote

51

For Delete, you will need to do as per the following

axios.delete("/<your endpoint>", { data:<"payload object">}) 

It worked for me.

Top 3 video Explaining javascript - Axios Delete request with body and headers?

Related QUESTION?