Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

python - 获取烧瓶请求中收到的数据(Get the data received in a Flask request)

I want to be able to get the data sent to my Flask app.

(我希望能够将数据发送到我的Flask应用。)

I've tried accessing request.data but it is an empty string.

(我尝试访问request.data但是它是一个空字符串。)

How do you access request data?

(您如何访问请求数据?)

@app.route('/', methods=['GET', 'POST'])
def parse_request():
    data = request.data  # data is empty
    # need posted data here

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.

(这个问题的答案使我提出了在Python Flask中获取原始POST正文的问题,而不管接下来的Content-Type标头如何 ,这都是关于获取原始数据而不是已解析数据的。)

  ask by ddinchev translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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将为空,因为它被用作后备:)

request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.

(request.data包含传入的请求数据(如果字符串带有Flame无法处理的mimetype),则为字符串。)

  • request.args : the key/value pairs in the URL query string

    (request.args :URL查询字符串中的键/值对)

  • request.form : the key/value pairs in the body, from a HTML post form, or JavaScript request that isn't JSON encoded

    (request.form :正文中的键/值对,来自HTML帖子形式或非JSON编码的JavaScript请求)

  • request.files : the files in the body, which Flask keeps separate from form .

    (request.files :主体中的文件,Flask与form分开。)

    HTML forms must use enctype=multipart/form-data or files will not be uploaded.

    (HTML表单必须使用enctype=multipart/form-data否则将不会上传文件。)

  • request.values : combined args and form , preferring args if keys overlap

    (request.valuesargsform组合,如果键重叠,则首选args)

  • request.json : parsed JSON data.

    (request.json :解析的JSON数据。)

    The request must have the application/json content type, or use request.get_json(force=True) to ignore the content type.

    (该请求必须具有application/json内容类型,或使用request.get_json(force=True)忽略该内容类型。)

All of these are MultiDict instances (except for json ).

(所有这些都是MultiDict实例( json除外)。)

You can access values using:

(您可以使用以下方法访问值:)

  • request.form['name'] : use indexing if you know the key exists

    (request.form['name'] :如果知道密钥存在,请使用索引)

  • request.form.get('name') : use get if the key might not exist

    (request.form.get('name') :如果密钥可能不存在,请使用get)

  • request.form.getlist('name') : use getlist if the key is sent multiple times and you want a list of values.

    (request.form.getlist('name') :如果多次发送密钥并且您想要一个值列表,请使用getlist 。)

    get only returns the first value.

    (get仅返回第一个值。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...