从请求响应中获取JSON

时间:2020-06-22 07:55:23

标签: json python-3.x flask

在对我的第一个使用flask_restful和python3.7的API进行的集成测试中,我尝试检查响应的内容。在互联网上进行的快速搜索中,我发现response.json()应该可以解决这个问题并返回字典,但是最接近该属性的是属性response.json,该属性返回一个字符串。我知道我可以使用json.loads( response.json ),但是我试图理解为什么我的回复没有像json()text()这样的方法。这是版本吗?

from todo_api import app
import unittest
import json

class IntegrationTest(unittest.TestCase):
    def setUp(self):
        self.app = app.test_client()

    def test_get(self):
        response = self.app.get('/todos')
        assert response.status_code == 200
        assert response.is_json
        assert json.loads( response.json)["1"] == "test" #this works
        assert response.json()["1"] == "test" #this would be nicer
 
if __name__ == "__main__":
    unittest.main()

0 个答案:

没有答案