Axios Delete Request With Body And Headers?
Answer :
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 } }); 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
Here is a brief summary of the formats required to send various http verbs with axios:
GET: Two waysFirst 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
paramskeyword in the second method.POSTandPATCHaxios.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 )DELETEaxios.delete('url', { data: payload }).then( // Observe the data keyword this time. Very important // payload is the request body // Do something )
Key take aways
getrequests optionally need aparamskey to properly set query parametersdeleterequests with a body need it to be set under adatakey
Comments
Post a Comment