如何修复flask test_client().post()中的json解码错误?

2024-10-04 09:27:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图为FlaskJSONAPI编写一个测试套件,但似乎无法将{"form_id": "data"}传递到app.test_client() post()方法的json=参数中。在

{"form_i": "data"}工作正常,我尝试过设置各种编码选项,但没有任何运气。在

with app.test_client() as c:
  test_call = c.post("api/signup/", json={'form_id': 'hi'})

给出以下错误消息:

^{pr2}$

Tags: 方法testformclientidjsonapp编码
2条回答

把这个推荐给你

import json
with app.test_client() as c:
  test_call = c.post("api/signup/", 
                     data=json.dumps({'form_id': 'hi'}), 
                     content_type='application/json')
^{pr2}$

Nevermind,原来是另一个错误-调用了一个单独的JSON文件,忘记删除一个尾随逗号。。。

相关问题 更多 >