获取烧瓶请求中收到的数据 [英] Get the data received in a Flask request
问题描述
我希望能够将数据发送到我的Flask应用程序.我尝试访问request.data
,但是它是一个空字符串.您如何访问请求数据?
I want to be able to get the data sent to my Flask app. I've tried accessing request.data
but it is an empty string. How do you access request data?
from flask import request
@app.route('/', methods=['GET', 'POST'])
def parse_request():
data = request.data # data is empty
# need posted data here
这个问题的答案使我问无论内容类型标题如何,都使用Python Flask获取原始POST正文,接下来是要获取原始数据而不是解析后的数据.
The answer to this question led me to ask Get raw POST body in Python Flask regardless of Content-Type header next, which is about getting the raw data rather than the parsed data.
推荐答案
文档描述请求中可用的属性.在大多数情况下,request.data
将为空,因为它用作后备:
The docs describe the attributes available on the request. In most common cases request.data
will be empty because it's used as a fallback:
request.data
如果传入的请求数据带有Flame无法处理的mimetype,则将输入的请求数据包含为字符串.
request.data
Contains the incoming request data as string in case it came with a mimetype Flask does not handle.
-
request.args
:URL查询中的键/值对字符串 -
request.form
:主体中的键/值对,来自HTML发布表单或未经JSON编码的JavaScript请求 -
request.files
:Flask保留在正文中的文件与form
分开. HTML表单必须使用enctype=multipart/form-data
,否则将不会上传文件. -
request.values
:组合了args
和form
,如果键重叠,则首选args
-
request.json
:已解析的JSON数据.该请求必须具有application/json
内容类型,或使用request.get_json(force=True)
忽略内容类型. request.args
: the key/value pairs in the URL query stringrequest.form
: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn't JSON encodedrequest.files
: the files in the body, which Flask keeps separate fromform
. HTML forms must useenctype=multipart/form-data
or files will not be uploaded.request.values
: combinedargs
andform
, preferringargs
if keys overlaprequest.json
: parsed JSON data. The request must have theapplication/json
content type, or userequest.get_json(force=True)
to ignore the content type.-
request.form['name']
:如果知道密钥存在,请使用索引 -
request.form.get('name')
:如果密钥可能不存在,请使用get
-
request.form.getlist('name')
:如果多次发送密钥并且您需要值列表,请使用getlist
.get
仅返回第一个值. request.form['name']
: use indexing if you know the key existsrequest.form.get('name')
: useget
if the key might not existrequest.form.getlist('name')
: usegetlist
if the key is sent multiple times and you want a list of values.get
only returns the first value.
所有这些都是 MultiDict
实例(除了
All of these are MultiDict
instances (except for json
). You can access values using:
这篇关于获取烧瓶请求中收到的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!