如何获取Flask请求中收到的数据 [英] How to get data received in Flask request

查看:1593
本文介绍了如何获取Flask请求中收到的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够将数据发送到我的Flask应用程序。我试过访问 request.data ,但它是一个空字符串。如何访问请求数据?

  @ app.route('/',methods = ['GET','POST' ])
def parse_request():
data = request.data#data is empty
#需要在这里发布数据






这个问题的答案让我要求接下来,获取原始数据而不是解析数据,然后在Python Flask中获取原始的POST正文。

文档描述了请求中可用的属性。在大多数情况下, request.data 将是空的,因为它被用作后备:


<包含传入的请求数据作为字符串,以防带有mimetype Flask无法处理。

$ $ request.data
$ b


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

  • request.form :正文中的键/值对,HTML帖子表单或未经JSON编码的JavaScript请求

  • request.files :Flask与 form 分开的正文中的文件。 HTML表单必须使用 enctype = multipart / form-data 或者文件不会被上传。
  • 请求。值:合并 args 表格,首选 args 如果键重叠


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?

@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.

解决方案

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 Contains the incoming request data as string in case it came with a mimetype Flask does not handle.

  • request.args: the key/value pairs in the URL query string
  • request.form: the key/value pairs in the body, from a HTML post form, or JavaScript request that isn't JSON encoded
  • request.files: the files in the body, which Flask keeps separate from form. HTML forms must use enctype=multipart/form-data or files will not be uploaded.
  • request.values: combined args and form, preferring args if keys overlap

这篇关于如何获取Flask请求中收到的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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