Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

axios.delete - must specify 'data' object? #736

Closed
AkiraLaine opened this issue Mar 3, 2017 · 22 comments
Closed

axios.delete - must specify 'data' object? #736

AkiraLaine opened this issue Mar 3, 2017 · 22 comments

Comments

@AkiraLaine
Copy link

When using the alias axios.delete, the payload doesn't get sent with the API call if you don't specify data.

You have to do:

axios.delete(URL, {
 data: { foo: 'bar' }
})

instead of

axios.delete(URL, { foo: 'bar' })

According to this, you shouldn't have to define data. I've been able to use other methods with payload without specifying data.

@charlesrochati
Copy link

charlesrochati commented Mar 3, 2017

Nope, this won't work.

What you have to pass to Axios is a Request Config Object and in this object you can find the data and params properties. You will use them to send data to the server.

This way: axios.delete(URL, {params: {foo: 'bar'}})


In a delete request you should use params instead of data, see the docs:

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

data: { firstName: 'Fred' },

@AkiraLaine
Copy link
Author

@charlesrochati I'm confused. params is to send query string parameters, that's not what I'm doing.
As for what you quoted above, it says nothing about delete.

@charlesrochati
Copy link

charlesrochati commented Mar 5, 2017

params is to send query string parameters, that's not what I'm doing.

If you want to make a axios.delete, that's exactly what you have to do.

You don't use axios.delete(URL, {
data: { foo: 'bar' } //Only applicable for request methods 'PUT', 'POST', and 'PATCH'
})
for a delete request, you will use axios.delete(URL, {
params: { foo: 'bar' }
})

You will send data as the request body ONLY WHEN you are using 'PUT', 'POST', and 'PATCH'.

I'm afraid you're using axios.delete just like a axios.post or (axios.put or axios.patch). When using axios.delete, you'll send an id to a server, and the server itself will find a resource that matches that given id, only then it will be removed (or trigger some action, but the request is mapped as a http delete).

By using axios.delete(URL, {
data: { foo: 'bar' }
}) you're sending a resource, which makes no sense at all.

Take a look at the links below, you will have a better understanding:

http://stackoverflow.com/questions/12142652/what-is-the-usefulness-of-put-and-delete-http-request-methods

https://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html [Section 9.7]

@jcready
Copy link
Contributor

jcready commented Mar 11, 2017

The HTTP DELETE method should not have a request body. DELETE is telling the origin server to remove the resource identified by the URI.

@AkiraLaine
Copy link
Author

AkiraLaine commented Mar 12, 2017

In my case, I do give a payload as well. But either way, if it works using data, it should work using the shorthand.

EDIT: The specification does not disallow/forbid passing a payload.

@charlesrochati
Copy link

There is no shorthand for axios.delete:

axios.request(config)

axios.get(url[, config])

axios.delete(url[, config])

axios.head(url[, config])

axios.post(url[, data[, config]])

axios.put(url[, data[, config]])

axios.patch(url[, data[, config]])

@jcready
Copy link
Contributor

jcready commented Mar 12, 2017

@AkiraLaine the specification also doesn't forbid sending request bodies for GET either. Should axios also allow you to use the short-hand notation for sending request bodies in GET requests as well?

@rubennorte
Copy link
Member

I'm closing the issue as it's the expected behaviour.

@cggaurav
Copy link

@rubennorte I think we should support body in DEL
HTTP 1.1
And for an example, Elastic

@jcready
Copy link
Contributor

jcready commented Apr 27, 2017

@cggaurav it is supported.

axios.delete('/some/uri', { body: 'my delete body' })

@aricsangchat
Copy link

On a side note, On the server the id is showing up in req.query instead of req.params when using axios.delete('/some/uri', {params: { id: 1 } }) like this. Just wanted to throw that out there.

@jcready
Copy link
Contributor

jcready commented Jun 9, 2017

@aricsangchat that's likely because you're using express which is not affiliated with axios at all. When express refers to params it is talking about dynamic values matched in the request path. When axios refers to params it is referring to query string parameters.

// Express
router.get('/:foo/:bar', (req, res) => {
  res.json({
    query: req.query,
    params: req.params
  })
})
// Axios
axios.get('/some/uri', { params: { id: 1 } })
.then((response) => console.log(response.data))

The output you'd get from running that would be:

{
  "query": { "id": 1 },
  "params": {
    "foo": "some",
    "bar": "uri"
  }
}

@aricsangchat
Copy link

@jcready Yup thats our setup.

@rudovjan
Copy link

Where is written that delete cannot have a body (like post)? It would be nice if axios supports this https://tools.ietf.org/html/draft-ietf-httpbis-p2-semantics-19#page-23

Thank you

@jcready
Copy link
Contributor

jcready commented Aug 28, 2017

It does support it.

@duhseekoh
Copy link

duhseekoh commented Oct 16, 2017

FYI. Fair points that DELETE should not have a body. However, sometimes you're stuck having to send a body when the api is out of your control. e.g.
https://auth0.com/docs/api/authorization-extension#remove-user-from-roles 👎

@jcready
Copy link
Contributor

jcready commented Oct 16, 2017

@duhseekoh

const extension_url = 'mycompany.auth0.com'
const access_token = 'abc123'
const user_id = 12345
const role_id = 67890

axios.delete(`https://${extension_url}/users/${role_id}/roles`, {
  headers: {
    'Authorization': `Bearer ${access_token}`,
    'Content-Type': 'application/json'
  },
  body: JSON.stringify([
    user_id
  ])
})

@duhseekoh
Copy link

duhseekoh commented Oct 16, 2017

@jcready - For sure and thanks for the example. Just wanted to add some reasoning for why a body may need to be passed on DELETE when using axios.

@mrchief
Copy link

mrchief commented Nov 14, 2017

@duhseekoh Maybe this will help: #897 (comment)

@quinnliu
Copy link

quinnliu commented Apr 2, 2018

const response = await axios.delete('/api/matches/delete_match', {
		data: { matchInfo }
	});

I have it working with data but not working with params with version "axios": "^0.17.1"

@HanchengZhao
Copy link

@quinnliu I think choosing to use data or params depends on how your backend handles the request.

  • Using data is to put the payload inside request body, just as you do with post.
  • Using params is to treat payloads as url params. Like if you send
axios.delete(URL, {
 params: { foo: 'bar' }
})

it's same as sending request to URL?foo=bar.

@jrbytes
Copy link

jrbytes commented Feb 19, 2020

Abstraction from axios in api. Params in data: name only

api.delete(`/projects/${idProjectTech}/techs`, {data: { name: data.name }})

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests