Flask - 当内容类型为“application / x-www-form-urlencoded”时,如何读取POST请求中的原始主体。 [英] Flask - How do I read the raw body in a POST request when the content type is "application/x-www-form-urlencoded"
问题描述
原来,如果请求的内容类型是 application / x-www-form-1,Flask将
。由于我正在使用JSON正文请求,我只想分析json或强制Flask解析它并返回 request.data
了urlencoded request.json
。
这是必须的,因为更改AJAX内容类型会强制使用HTTP OPTION请求,这会使后端变得复杂。
如何使Flask返回请求对象中的原始数据?
您可以通过 request.form获取发布数据。如果内容类型是
。 application / x-www-form-urlencoded
,那么keys()[0]
request.form
是一个 multidict ,其键包含解析后的数据。
Turns out that Flask sets request.data
to an empty string if the content type of the request is application/x-www-form-urlencoded
. Since I'm using a JSON body request, I just want to parse the json or force Flask to parse it and return request.json
.
This is needed because changing the AJAX content type forces an HTTP OPTION request, which complicates the back-end.
How do I make Flask return the raw data in the request object?
You can get the post data via request.form.keys()[0]
if content type is application/x-www-form-urlencoded
.
request.form
is a multidict, whose keys contain the parsed post data.
这篇关于Flask - 当内容类型为“application / x-www-form-urlencoded”时,如何读取POST请求中的原始主体。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!