Axios Delete request with body and headers?

351,292

Solution 1

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
  }
});

Solution 2

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

Solution 3

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

Solution 4

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})

Solution 5

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

Share:
351,292

Related videos on Youtube

Asfourhundred
Author by

Asfourhundred

Updated on October 22, 2021

Comments

  • Asfourhundred
    Asfourhundred over 2 years

    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!

  • Asfourhundred
    Asfourhundred almost 6 years
    This does not work for me... I have const headers = {'Authorization': ...} and data = {'username': ...} ending up with axios.delete('http://...', {headers, data}) but the server cannot access the headers...
  • Asfourhundred
    Asfourhundred almost 6 years
    The thing is I want to send a body and headers in the same delete request
  • Oluwafemi Sule
    Oluwafemi Sule almost 6 years
    The request going out from the browser says different. See this Stackblitz (stackblitz.com/edit/react-gq1maa) and also the request in the browser network tab (snag.gy/JrAMjD.jpg). You need to be sure here that you're reading the headers server side the right way or that the request isn't intercepted and tampered with.
  • eli-bd
    eli-bd about 5 years
    Your answer makes me wish there was a +2 upvote feature on stack overflow.
  • Geoff
    Geoff about 4 years
    This is the only answer that explains it in details. Thanks it really helped in understanding even others.
  • Ajay Singh
    Ajay Singh almost 4 years
    How to send delete request with params, not body?
  • HartleySan
    HartleySan almost 4 years
    Best answer to this question. Thank you.
  • shanti
    shanti almost 4 years
    Thanks, I am using this in my nestJs HttpService delete method as : this.httpService.delete(apiUrl, { headers: headersRequest, data: deleteBody })
  • Van_Paitin
    Van_Paitin over 3 years
    @MaFiA, if you want to send delete request with params. You can simply put it in the url using query strings
  • nimsrules
    nimsrules over 3 years
    Great explanation, saved me from a potential headache. Thank you!
  • geoidesic
    geoidesic over 3 years
    I can't get this to work. Sending axios.delete('some-url',{data: someObj}) sends a DELETE message with no body.
  • Van_Paitin
    Van_Paitin over 3 years
    someObj is probably null or undefined. Ensure nothing has edited someObj before sending the request
  • Amar Ratna Shakya
    Amar Ratna Shakya over 3 years
    It is not working i send {data: payload} for delete but still it won't send any request body
  • Franco Gil
    Franco Gil over 3 years
    Hello, can you explain why your answer works?
  • Harshit Gangwar
    Harshit Gangwar about 3 years
    worked for me, I'm using React and Django
  • Namrata Das
    Namrata Das about 3 years
    Possibly because DELETE should not have request bodies. Maybe there's something in there that prevents this (as it should)
  • Victor Pudeyev
    Victor Pudeyev almost 3 years
    @Evert that's incorrect, DELETE requests have not defined semantics for the body, so you can have the request body, but old implementations may reject the request. IMO you should have the request body, and obsolete the old clients and rotate new clients in their place.
  • Namrata Das
    Namrata Das almost 3 years
    @VictorPudeyev hey, I understand that the language in the HTTP specification is confusing. Yes a body may appear, but it should have any meaning to the server. So there is never a good reason to add a body to a HTTP DELETE body. So you can add a body, but it's pointless.
  • Namrata Das
    Namrata Das almost 3 years
    So my original comment is correct. In fact, this a paragraph from the upcoming HTTP spec that echoes this: "A client SHOULD NOT generate content in a DELETE request. Content received in a DELETE request has no defined semantics, cannot alter the meaning or target of the request, and might lead some implementations to reject the request."
  • Namrata Das
    Namrata Das almost 3 years
    @VictorPudeyev Disclaimer, I helped write this. Reference: httpwg.org/http-core/…
  • lsr
    lsr over 2 years
    This worked for me. Thanks!
  • Michael Jay
    Michael Jay over 2 years
    @Evert - in your comment on Jun 3 17:11, did you mean "but it SHOULDN'T have any meaning"?
  • Namrata Das
    Namrata Das over 2 years
    @MichaelJay yes, thank you for pointing that out!