Axios: Support delete body

Created on 13 May 2017  ·  7Comments  ·  Source: axios/axios

This is probably a design choice by the axios team and for good reason but I write all my POST/GET/PUTs like this:

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

And my server sometimes expects data inside the DELETE request body (which may not be technically according to spec but nonetheless it is common) in which case I have to write my DELETE in this form:

      const res = await axios({
        method: 'DELETE',
        url: 'url',
        data: {
          ...
        }
      })

Which works fine but I'm forced to use the second syntax because the axios.delete doesn't support a request body.

So I would like to request that axios.delete will support a request body.

Thanks

Most helpful comment

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

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

post, put, and patch accept 3 parameters: url, data, and config so you can you can use the second parameter to set the response body like this:

axios.put(url, { foo: "bar" });

Hope it helps!

All 7 comments

We often use data with GET requests on the server and they are translated to query params. The lack of data support on certain methods is a big downside for axios in my hopes of more isomorphic code.

Thanks, Here I found this answer

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

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

post, put, and patch accept 3 parameters: url, data, and config so you can you can use the second parameter to set the response body like this:

axios.put(url, { foo: "bar" });

Hope it helps!

I think the README is making it dificult to understand:

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  // When no `transformRequest` is set, must be of one of the following types:
  // - string, plain object, ArrayBuffer, ArrayBufferView, URLSearchParams
  // - Browser only: FormData, File, Blob
  // - Node only: Stream, Buffer
  data: {
    firstName: 'Fred'
  },

This part: // Only applicable for request methods 'PUT', 'POST', and 'PATCH' especially.

please add this comment to the docs

It's on the docs https://github.com/axios/axios#request-method-aliases

what is the difference between these two?

this.$axios.$delete(queryData.url,
        {
          params: { id: String(queryData.id) }
        })
this.$axios.$delete(queryData.url,
        {
          body: { id: String(queryData.id) }
        })

[params] worked on my side

Was this page helpful?
0 / 5 - 0 ratings