gpt4 book ai didi

python - Flask 或 Werkzeug/0.9.4 破坏 POST 数据

转载 作者:行者123 更新时间:2023-12-01 05:30:30 25 4
gpt4 key购买 nike

我发现 Flask-restful 的一些行为我认为是由 Werkzeug/0.9.4 引起的,但我不明白。当我尝试 POST 包含“=”的有效 JSON 时,Multidict 的使用似乎破坏了我的数据。

这是我的测试 JSON:

{
"alert": {
"@id": "90",
"action": "hoojimaflip",
"fruit": {
"@bowl": "bananas",
"@protocol": "tcp"
},
"url": "https://this-is-a-sample/paramer?id=90"
}
}

这是 POST 方法。

def post(self):
f1=open("./log", 'w+')
data = request.json
if not data:
# I know this is not equivalent to the JSON above.
# Just troubleshooting by dumping it all out.
data = request.form
print >>f1, data
return ('', 201)

如果我使用 cURL 和 application/json 进行 POST 就可以了。我在 request.data 中正确获取了 POSTed JSON。稍后我需要将其渲染回 JSON,但没问题。

{
u'alert': {
u'@id': u'90'
u'action': u'hoojimaflip',
u'fruit': {
u'@bowl': u'bananas',
u'@protocol': u'tcp'
},
u'url': u'https://this-is-a-sample/paramer?id=90',
}
}

如果我通过带有 application/x-www-form-urlencoded 的 cURL 发布,那么我应该能够获取 request.form 中的数据。但是,似乎有什么东西破坏了我的数据。

ImmutableMultiDict([('
{ "alert": {
"@id": "90",
"action": "hoojimaflip",
"fruit": {
"@bowl": "bananas",
"@protocol": "tcp"
},
"url": "https://this-is-a-sample/paramer?id', u'90"
}
}'
)])

“=”符号被用作某种记录分隔符并破坏了 POSTed JSON。

有人有什么想法吗?我是否遗漏了一些明显的东西?

谢谢!

最佳答案

如果外部应用程序顽固地使用替代 mime 类型进行 POST-ing,您可以通过使用 request.get_json() method 强制 Flask 将数据视为 JSON 无论如何相反,将 force 参数设置为 True:

data = request.get_json(force=True)

不要尝试将 JSON 负载视为表单数据,那永远不会起作用。

关于python - Flask 或 Werkzeug/0.9.4 破坏 POST 数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20381722/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com