I am building a react-django simple blog app and I am trying to delete blog post but I also want to send body with delete to backend But I have no idea, How can I access delete body ?
. I can do with post like self.request.POST
with how with delete
?
App.js:
class BlogPost extends React.Component {
deleteBlog = (blog_title) => {
const body = ({title: blog_title});
const headers = {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
}
axios.delete("delete_blog/", blog_title, {headers:headers}).then(res => {console.log})
}
render() {
return (
<div>
{
this.state.blogs.map(res => <div>
{res.blog_title}
<button onClick={() => deleteBlog(res.blog_title)}></button>
</div>
}
</div>
)}}
views.py:
class BlogPost(APIView):
def post(self, *args, **kwargs):
.......
def delete(self, *args, **kwargs):
# Trying to access delete body here......
print(self.request.POST)
# It printed empty dict like <[QueryDict = {}]>
I have tried many times but it is still not showing.
CodePudding user response:
To Use Axios Delete request with body , you need to use axios.delete(url,{ headers: { "Authorization": "***" }, data: { } } );
eg:
axios.delete("delete_blog/", {
headers: headers,
data: {
source: blog_title
}
});