简体   繁体   English

如何检查响应的状态码更改为 json?

[英]How to check status code of response changed to json?

i have a question.我有个问题。 I am doing rtsp client tests.我正在做 rtsp 客户端测试。 This Client is our, works preety nice.这个客户是我们的,工作得很好。 Question is, is exist a way to check status of response if it return in json?问题是,如果返回 json,是否存在检查响应状态的方法?

client.py客户端.py

def get_paths(self):
   return requests.get(f"{self.API_URL}/v1/paths/list").json()

tests.py测试.py

def test_get_path(self):
    response = self.rtsp_client.get_paths()
    self.assertEqual(response, 200)

In this example, i got:在这个例子中,我得到:

Traceback (most recent call last):
  File "/home/oliwer/Documents/repozytoriumGit/device-channels/project/backend/rtsp_simple_server_api/tests.py", line 99, in test_get_path
    self.assertEqual(response, 200)
AssertionError: {'items': {'none': {'confName': 'none', '[3105 chars][]}}} != 200

I understand why i got this output. I am just interested is exist a way to test status code of this response.我明白为什么我得到这个 output。我只是感兴趣是否存在一种方法来测试此响应的状态代码。

Ps.附言。 The point is that, i can't change request functionality, it has to return json, i wanted to get something like that:关键是,我无法更改请求功能,它必须返回 json,我想得到类似的东西:

tests.py测试.py

    def test_get_path(self):
       response = self.rtsp_client.get_path().command_that_can_reverse_json()_command
       self.assertEqual(response, 200)

I thought, that it is possible, when i have.json() method.我想,当我有 .json() 方法时,这是可能的。 In my opinion response is an object with this first response, but encoded to json file.在我看来,第一个响应是 object,但编码为 json 文件。 So my thoughts were that i can reverse this operation, but i did not know how.所以我的想法是我可以逆转这个操作,但我不知道怎么做。 In your opinion, i can't do that.在你看来,我做不到。

I would suggest, that you change your code like this我建议您像这样更改代码

def get_paths():
   return requests.get('https://jsonplaceholder.typicode.com/todos/1')

than you can access the status_code of the response with:比您可以访问响应的状态代码:

r = get_paths()
print(r.status_code)
>>> 200

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM