我想用pytest将带有POST请求的json发送到特定的端点到我的服务器。
这就是我通常用curl给服务器提供的内容:
curl --header "Content-Type: application/json" --request POST --data '{"item":"ZycugK6yXdHgf8foQTUnKY.txt","method":"one"}' 0.0.0.0:5000/convert这就是我试图在pytest中重新创建它的方法:
data = {"item": filename, "method": method}
response = client.post('/convert', content_type='application/json', data=data)其中接收文件名和方法参数作为函数的输入(由修饰器转发)。
但是,我一直得到一个400 - Bad Request,这意味着我的请求做错了什么。
发布于 2021-03-04 09:49:11
我认为这是可行的:
data = {"item": filename, "method": method}
response = client.post('/convert', content_type='application/json', data=json.dumps(data))您需要序列化数据。
发布于 2021-03-04 09:55:30
这取决于您正在使用的客户端。对于requests,您可以简单地使用
response = client.post("/convert", json=data)https://stackoverflow.com/questions/66472433
复制相似问题