Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Axios Delete request with body and headers?

I'm using Axios while programing in ReactJS and I pretend to send a DELETE request to my server.

To do so I need the headers:

headers: {
  'Authorization': ...
}

and the body is composed of

var payload = {
    "username": ..
}

I've been searching in the inter webs and only found that the DELETE method requires a "param" and accepts no "data".

I've been trying to send it like so:

axios.delete(URL, payload, header);

or even

axios.delete(URL, {params: payload}, header);

But nothing seems to work...

Can someone tell me if its possible (I presume it is) to send a DELETE request with both headers and body and how to do so ?

Thank you in advance!

like image 450
Asfourhundred Avatar asked Jun 27 '18 18:06

Asfourhundred


People also ask

Can we send body in delete request Axios?

To Use Axios Delete request with body and headers In ReactJS You Just need to Use axios. delete{URL,{headers:{},data:{}}} This Structure, Where You can Pass Authorization, and etc Params That you want to pass in your headers, and pass all body params in data{}. Now, You Can Easily Use Delete Request. Thank You.

Can we send body in delete request?

A message-body MUST NOT be included in a request if the specification of the request method (section 5.1. 1) does not allow sending an entity-body in requests.

How do you send a delete request using Axios?

DELETE request using axios with async/await This sends the same DELETE request using axios, but this version uses an async function and the await javascript expression to wait for the promises to return (instead of using the promise then() method as above).

Should HTTP delete have a body?

By this it seems optional whether you want to provide a body for a DELETE request. The RFC states that: A payload within a DELETE request message has no defined semantics; sending a payload body on a DELETE request might cause some existing implementations to reject the request.


8 Answers

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
  }
});
like image 148
vishu2124 Avatar answered Sep 28 '22 11:09

vishu2124


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

like image 26
tarzen chugh Avatar answered Sep 28 '22 12:09

tarzen chugh


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
like image 44
Van_Paitin Avatar answered Sep 28 '22 13:09

Van_Paitin


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})
like image 32
Oluwafemi Sule Avatar answered Sep 28 '22 13:09

Oluwafemi Sule


For those who tried everything above and still don't see the payload with the request - make sure you have:

"axios": "^0.21.1" (not 0.20.0)

Then, the above solutions work

axios.delete("URL", {
      headers: {
        Authorization: `Bearer ${token}`,
      },
      data: {
        var1: "var1",
        var2: "var2"
      },
    })

You can access the payload with

req.body.var1, req.body.var2

Here's the issue:

https://github.com/axios/axios/issues/3335

like image 22
x4wiz Avatar answered Sep 28 '22 12:09

x4wiz


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

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

It worked for me.

like image 36
Hemantkumar Gaikwad Avatar answered Sep 28 '22 12:09

Hemantkumar Gaikwad


I had the same issue I solved it like that:

axios.delete(url, {data:{username:"user", password:"pass"}, headers:{Authorization: "token"}})
like image 20
ronara Avatar answered Sep 28 '22 11:09

ronara


Actually, axios.delete supports a request body.
It accepts two parameters: a URL and an optional config. That is...

axios.delete(url: string, config?: AxiosRequestConfig | undefined)

You can do the following to set the response body for the delete request:

let config = { 
    headers: {
        Authorization: authToken
    },
    data: { //! Take note of the `data` keyword. This is the request body.
        key: value,
        ... //! more `key: value` pairs as desired.
    } 
}

axios.delete(url, config)

I hope this helps someone!

like image 25
ThunderBird Avatar answered Sep 28 '22 12:09

ThunderBird