返回信息流RT。flask在数据请求获取数据的时候,对于request.form和request.get_json具体有什么不同呢?感觉都是表单啊,感觉get_json只是字符串形式的表单。。。求解!!
这是一条镜像帖。来源:北邮人论坛 / python / #20988同步于 2018/2/6
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖
【求助】get_json和form
hotpot
2018/2/6镜像同步6 回复
订阅后,新回复会通过你的通知中心匿名送达。
6 条回复
只用过.form方法。我的做法是在模板html中发送POST请求,然后再Python中用request.form['key']收到提交的内容,然后再处理就行了。
其实form和json我都用过,但是就是不知道有什么区别。。。
【 在 fuxuemingzhu 的大作中提到: 】
: 只用过.form方法。我的做法是在模板html中发送POST请求,然后再Python中用request.form['key']收到提交的内容,然后再处理就行了。
稍微查了下flask的官方文档。
对于form的描述如下:
A MultiDict with the parsed form data from POST or PUT requests. Please keep in mind that file uploads will not end up here, but instead in the files attribute.
对get_json函数的说明如下:
By default this function will only load the json data if the mimetype is application/json but this can be overriden by the force parameter
可以看出来这两种是针对不同的请求类型的响应。从描述看form应该是接收content-type为application/x-www-form-urlencoded的数据(典型的如js中用form的submit方法),get_json接收content-type为application/json的数据(例如用requests库做post前先将数据dumps为json格式)。
楼主可以自己动手构造下这两个请求,看下服务器接收的内容就明白了。
这个也就是说head不同喽。。。
【 在 lairen 的大作中提到: 】
: 稍微查了下flask的官方文档。
: 对于form的描述如下:
: A MultiDict with the parsed form data from POST or PUT requests. Please keep in mind that file uploads will not end up here, but instead in the files attribute.
: ...................