[英]Django Rest - testing api delete
Currrently I am testing my django rest api, but I am stuck on delete method.目前我正在测试我的 django rest api,但我被困在删除方法上。
My url looks like this我的网址看起来像这样
path('books/shelfs/<int:shelf>/readers/<int:pk>/',
views.ReaderViewSet.as_view(
{'get': 'retrieve', 'delete': 'destroy', 'patch': 'partial_update'}),
And My ViewSets looks like this我的 ViewSets 看起来像这样
def destroy(self, request, pk=None, *args, **kwargs):
if request.data.get("book_type") is None:
raise ParseError(detail="book_type is required, options are : 'global, non-global'")
try:
instance = self.get_object()
user = self.request.user
serializer = self.get_serializer(self.get_object())
.......
self.perform_destroy(instance)
except Http404:
pass
return Response(status=status.HTTP_204_NO_CONTENT)
And my test case is this我的测试用例是这样的
def test_book_delete(self):
# check if delete works
book_type = {'book_type': 'global'}
response = self.client.delete("/api/v1/books/shelfs/{}/"
"readers/{}/".format(
1, 2), data=book_type)
self.assertEqual(response.status_code, 204)
But its alway 415 error但它总是 415 错误
The question is, how to pass this book_type
in delete ?问题是,如何在 delete 中传递这个
book_type
?
HTTP 415 means that the server refused to accept the JSON payload. HTTP 415表示服务器拒绝接受 JSON 负载。
If you need to explicitly encode the request body, you can do so by setting the content_type flag.
如果需要对请求正文进行显式编码,可以通过设置 content_type 标志来实现。
So try to set the content_type
as follows:所以尝试设置
content_type
如下:
response = self.client.delete("/api/v1/books/shelfs/{}/"
"readers/{}/".format(
1, 2), data=book_type, content_type="application/json")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.