简体   繁体   English

测试Post API接收状态码200而不是201

[英]test for post API receiving status code 200 instead of 201

My test for my post method API receive status code 200 but should 201 I tried to find where is problem but i couldn't. 我对发布方法API的测试收到状态代码200,但应该201我试图找到问题所在,但我没有。 It would be easier if i would get error status 4xx but i have no idea why post method could receive code 200. My API view works fine so this must be problem with test code but I really don't have idea what may cause this problem. 如果我将收到错误状态4xx,但我不知道为什么post方法为什么可以接收代码200,这会更容易。我的API视图工作正常,因此这一定是测试代码存在的问题,但是我真的不知道可能导致此问题的原因。 Maybe I'm wrong and status code 200 is ok, but I think that post method should receive status 201. 也许我错了,状态码200可以,但是我认为post方法应该接收状态201。

test_api.py test_api.py

def test_post_logged_in(self):
    product = Product.objects.get(id=1)
    self.client.login(username='test', password='test123')
    data = {
        'nick': self.user.id,
        'rate': '1/5',
        'content': 'here is comment',
        'product': product.id
    }
    response = self.client.post(reverse('add_comments', kwargs={'id': product.id}), data, format='json')
    self.assertEqual(response.status_code, 201, f'expected Response code 201, instead get {response.status_code}')

views.py views.py

class CreateComment(APIView):

def get_object(self, id):
    try:
        return Product.objects.get(id=id)
    except Product.DoesNotExist:
        raise Http404

def get(self,request,  id):
    product = self.get_object(id)
    serializer = ProductSerializer(product)
    return Response(serializer.data)

def post(self, request,id):
    serializer = CommentSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save(nick=request.user, product=self.get_object(id))
        return Response(serializer.data)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

In your post() , you need to return status.HTTP_201_CREATED if successful. post() ,您需要返回status.HTTP_201_CREATED如果成功。

Try this: 尝试这个:

def post(self, request,id):
    serializer = CommentSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save(nick=request.user, product=self.get_object(id))
        return Response(serializer.data, status=status.HTTP_201_CREATED)
    return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Django Rest 框架 API POST 接收状态码 200 而不是 201 - Django Rest Framework API POST receiving status code 200 instead 201 获取200状态代码而不是201或204 - Getting 200 status code instead of 201 or 204 Django API 测试:预期状态代码 200,而不是收到 301 - Django API test: expected status code 200, instead received 301 Python Flask Web服务器未收到JSON Ajax POST请求,服务器返回HTTP 200 OPTIONS而不是201 POST - Python Flask webserver not receiving JSON ajax POST requests, server returns HTTP 200 OPTIONS instead of 201 POST Python json POST请求状态为200,而预期状态为201 - Python json POST request status 200 while expecting status 201 为什么在我的帖子请求中得到返回码 200 而不是 201? (Python/请求/Json) - Why did I get return code 200 instead of 201 on my post request ? (Python /Requests/Json) Python requests.post() 方法命令返回 200 status_code 属性而不是 500 - Python requests.post() method command returns 200 status_code attribute instead of 500 FastAPI POST Pydantic 方法单元测试重定向 status_code 307 而不是 200 - FastAPI POST Pydantic method unit testing redirecting status_code 307 instead of 200 如何从 api 调用 json 响应中获取状态码 200 而不是 401 - How to get status code 200 instead of 401 from api calls json response 即使收到 200 状态代码,也会重试 Scrapy 请求 - Retrying a Scrapy Request even when receiving a 200 status code
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM