flask request args parser error浏览器(或代理)发送了此服务器无法理解的请求

2024-04-19 14:58:55 发布

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

使用test_客户端并发送如下请求:

app = Flask(__name__)
client = app.test_client()

headers = {'content-type': 'application/json', 'Authorization': u'Bearer fake_token_123'}
params = {'dont_care': True}
client.get(ֿֿ'/my_route/123', query_string=params, headers=headers)

我的路线是

^{pr2}$

parser.parse_args()失败

The browser (or proxy) sent a request that this server could not understand

'content-type': 'application/json'从header中移除时,它起作用。在

我不明白这种行为,如果没有不优雅的try, expect,我该如何防范它。在

谢谢你的帮助


Tags: nametestclientjsonapp客户端flaskapplication
1条回答
网友
1楼 · 发布于 2024-04-19 14:58:55

您已经发现了如何修复它:如果不发布JSON,请不要发送content-type: application/json。不能用GET发送JSON,即使可以(或使用POST),也必须先用json.dumps(data)对JSON进行编码。在

相关问题 更多 >