patternjavascriptreactCritical
Axios - DELETE Request With Request Body and Headers?
Viewed 0 times
headersdeleteaxiosandbodywithrequest
Problem
I'm using Axios while programming in ReactJS and I pretend to send a DELETE request to my server.
To do so I need the headers:
and the body is composed of
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:
or even
But nothing seems to work...
Can someone tell me if it's possible (I presume it is) to send a DELETE request with both headers and body and how to do so?
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 it's possible (I presume it is) to send a DELETE request with both headers and body and how to do so?
Solution
Here is a brief summary of the formats required to send various http verbs with axios:
-
-
First method
-
Second method
The two above are equivalent. Observe the
-
-
Key take aways
-
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 PATCHaxios.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 parameters
deleterequests with a body need it to be set under adatakey
Context
Stack Overflow Q#51069552, score: 227
Revisions (0)
No revisions yet.