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"

查看:2500
本文介绍了Flask - 当内容类型为“application / x-www-form-urlencoded”时,如何读取POST请求中的原始主体。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原来,如果请求的内容类型是 application / x-www-form-1,Flask将 request.data 了urlencoded 。由于我正在使用JSON正文请求,我只想分析json或强制Flask解析它并返回 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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆