Finally, in the following code we have the DELETE request, which is the simplest of the four supported methods. The main difference with the delete method is that it returns no content, which is the accepted standard with DELETE requests:
@jwt_required
def delete(self, post_id=None):
if post_id:
abort(400)
post = Post.query.get(post_id)
if not post:
abort(404)
if get_jwt_identity() != post.user_id:
abort(401)
db.session.delete(post)
db.session.commit()
return "", 204
Again, we can test using the following:
$ curl -X DELETE -H "Authorization: Bearer $ACCESS" http://localhost:5000/api/post/102
If everything is successfully deleted, you should receive a 204 status code and nothing should show up.
Before we move on from REST completely, there is one final challenge for you to test your understanding of Flask...