我发现了一些我认为是由 Werkzeug/0.9.4 引起的 Flask-restful 行为,我不明白。当我尝试发布包含“=”的有效 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)
如果我使用带有 application/json 的 cURL 发布,那很好。我在 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',
}
}
如果我通过 cURL 使用 application/x-www-form-urlencoded 发布,那么我应该能够获取 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。
有没有人有任何想法?我错过了一些明显的东西吗?
谢谢!