I have problem, when I send delete request to server using axios my body of request is empty, but with same code in post method it works. In postman delete method is working
Here id isn't empty
export const deleteClothes = async (id) => {
id.id = Number(id.id)
const {data} = await $authHost.delete('api/clothes', id)
return data
}
But here, req.body is empty
async delete(req, res) {
const{id} = req.body
await Clothes.destroy({
where:{
id: id
}
})
return res.json(`Clothes deleted: ${id}`)
}
The same post method:
export const createType = async (type) => {
const {data} = await $authHost.post('api/type', type)
return data
}
Idk what to do, I've tried a lot of things
CodePudding user response:
If we review the API documentation, we can see that the signature of the post method is axios.post(url[, data[, config]]), but the signature of the delete method is axios.delete(url[, config]) — notice that the second argument is a config object, not a request body as it is with post. You're passing the object you want used as the body directly as the config object.
Instead, pass a config object with a data property:
const {data} = await $authHost.delete('api/clothes', {data: id})
// −−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−−^^^^^^^^^^
Side note: It's confusing to have the deleteClothes parameter called id that's an object with an id property on it. In the code above, id is the object, which seems to be what you want given your post example. I'd suggest renaming the parameter to something reflecting what that object is (a clothing item or whatever).
CodePudding user response:
the DELETE method is not intended to have a request body coz not all requests have body. Instead you should include the information you want to delete in the query string or as a path parameter in the URL. for example api/clothes/clothID
