用烧瓶请求处理表单数据 [英] Handling form data with flask request
问题描述
我正在使用 flask-restful
作为api服务器,并构建第一个 PUT
方法。使用从flask导入的请求,我可以通过以下cURL命令访问 request.form
数据,而不会造成任何问题:
curl http://127.0.0.1:5000/api/v1/system/account -X PUT -d username = asdas -d email=asdasd@test.com
我的 PUT
方法注销用户名和电子邮件,
def put(self):
print'SystemAccountPut'
print request.form ['username' ]
print request.form ['email']
return
输出:
SystemAccountPut
asdas
asdasd@test.com
我有一个应用程序使用 axios项目来进行api调用。当axios试图 PUT
表单数据时, request.form
不再有效。这里是电话axios从Chrome开发控制台转换为cURL命令:
$ b $
curl'http://127.0.0.1: 5000 / api / v1 / system / account'-X PUT -H'Pragma:no-cache'-H'Origin:http://127.0.0.1:5000'-H'Accept-Encoding:gzip,deflate,sdch' -H'Accept-Language:en-US,en; q = 0.8' -H User-Agent:Mozilla / 5.0(Macintosh; Intel Mac OS X 10_11_3)AppleWebKit / 537.36(KHTML,如Gecko)Chrome / 48.0.2564.116 Safari / 537.36'-H'Content-Type:application / json; charset = UTF-8'-H'Accept:application / json,text / plain,* / *'-H'Cache-Control:no-cache' - H'Referer:http://127.0.0.1:5000/settings'-H'连接:keep-alive'--data-binary'{username:asdas,email:asdasd@test.com }'--compressed
使用上面相同的方法 request.form [ 'username']
和 request.form ['email']
为空。然而, request.data
中也有表单数据, request.get_json()
JSON格式。
我的问题是我应该在这种情况下使用什么来检索表单数据?第一个curl命令是用 request.form
来清理的,它包含我需要的数据,但是 request.data
是空的。第二个cURL命令会使 request.form
中断,但会填充 request.data
。在这两个cURL案例中,我应该如何检索表单数据是否有最佳做法?
在进一步了解新来的表单和来自大卫主义的一些洞察之后,我想到了这个问题。第一个cURL示例具有以下内容类型: application / x-www-form-urlencoded
。第二个cURL命令具有以下内容类型: application / json; charset = UTF-8
。不出所料,第一个cURL命令将表单数据发送到 request.form
,第二个cURL命令被解释为数据,可以在 request.data
或
request.get_json()
。为了我的需要,我希望以任何方式获取表单数据,所以在我的放置
方法中,我有以下几点:
data = request.get_json()或request.form
打印数据['email']
打印数据['username']
这给了我两个cURL示例中的电子邮件和密码。
I am using flask-restful
as an api server and am constructing the first PUT
method. Using request imported from flask, I am able to access request.form
data without issue with the following cURL command:
curl http://127.0.0.1:5000/api/v1/system/account -X PUT -d username=asdas -d email=asdasd@test.com
My PUT
method logs out both the username and email without issue:
def put(self):
print 'SystemAccountPut'
print request.form['username']
print request.form['email']
return
Output:
SystemAccountPut
asdas
asdasd@test.com
I have an app using the axios project to make api calls. When axios attempts to PUT
form data, request.form
no longer works. Here is the call axios is making converted to cURL command from Chrome Dev Console:
curl 'http://127.0.0.1:5000/api/v1/system/account' -X PUT -H 'Pragma: no-cache' -H 'Origin: http://127.0.0.1:5000' -H 'Accept-Encoding: gzip, deflate, sdch' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.116 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Cache-Control: no-cache' -H 'Referer: http://127.0.0.1:5000/settings' -H 'Connection: keep-alive' --data-binary '{"username":"asdas","email":"asdasd@test.com"}' --compressed
With the same method above request.form['username']
and request.form['email']
are empty. request.data
however has the form data in it, and request.get_json()
also will output the form data in JSON format.
My question is what should I be using in this case to retrieve the form data? The first curl command is clean with request.form
having the data I need, but request.data
is empty. The second cURL command leaves request.form
broken but does populate request.data
. Is there a best practice on how I should retrieve form data in both cURL cases?
I figured out the issue after further learning more about incoming forms and some insight from davidism. The first cURL example has the following Content-Type: application/x-www-form-urlencoded
. The second cURL command has the following Content-Type: application/json;charset=UTF-8
. Unsurprisingly, the first cURL command sends form data to request.form
and the second cURL command is interpreted as data and can retrieved at request.data
or request.get_json()
. For my needs I want to get the form data either way, so in my put
method I have the following:
data = request.get_json() or request.form
print data['email']
print data['username']
This gives me the email and password in both cURL examples.
这篇关于用烧瓶请求处理表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!