本文翻译自: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标头如何 ,这都是关于获取原始数据而不是已解析数据的。


#1楼

参考:https://stackoom.com/question/hmVz/获取烧瓶请求中收到的数据


#2楼

To get the raw data, use request.data . 要获取原始数据,请使用request.data This only works if it couldn't be parsed as form data, otherwise it will be empty and request.form will have the parsed data. 这仅在无法将其解析为表单数据时才有效,否则它将为空并且request.form将具有解析的数据。

from flask import request
request.data

#3楼

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仅返回第一个值。

#4楼

For URL query parameters, use request.args . 对于URL查询参数,请使用request.args

search = request.args.get("search")
page = request.args.get("page")

For posted form input, use request.form . 对于张贴的表单输入,请使用request.form

email = request.form.get('email')
password = request.form.get('password')

For JSON posted with content type application/json , use request.get_json() . 对于使用内容类型application/json发布的application/json ,请使用request.get_json()

data = request.get_json()

#5楼

If you post JSON with content type application/json , use request.get_json() to get it in Flask. 如果发布内容类型为application/json ,请使用request.get_json()在Flask中获取它。 If the content type is not correct, None is returned. 如果内容类型不正确,则返回None If the data is not JSON, an error is raised. 如果数据不是JSON,则会引发错误。

@app.route("/something", methods=["POST"])
def do_something():data = request.get_json()

#6楼

To get the raw post body regardless of the content type, use request.get_data() . 要获取原始帖子正文,而与内容类型无关,请使用request.get_data() If you use request.data , it calls request.get_data(parse_form_data=True) , which will populate the request.form MultiDict and leave data empty. 如果使用request.data ,它将调用request.get_data(parse_form_data=True) ,它将填充request.form MultiDict并将data保留为空。

获取烧瓶请求中收到的数据相关推荐

  1. Tornado的同步API写法举例实现GET/POST/DELETE请求+Tornado获取post请求中的json数据(转载)

    下面的实验主要来自[1][2],但是对实验2的代码进行了修改,修改过程参考了[3] #---------------------------------------------------实验1--- ...

  2. 获取http请求中的参数控制器给jsp传递数据的方式

    这里写自定义目录标题 获取http请求中的参数 直接参数名获取 通过对象的方式获取 通过Servlet API方式获取 当请求中的参数和方法中参数名不一致 直接在url中获取参数的方式 控制器给jsp ...

  3. 获取get请求中的参数

    需要获取get请求中的参数,将参数一一保存到数据库 方法一(只适合参数较少的情况): 使用 String a = request.getParameter("参数名");不适合参数 ...

  4. java获取jsp页面参数_jsp页面中获取servlet请求中的参数方法总结

    jsp页面中获取servlet请求中的参数的办法详解 在JAVA WEB应用中,如何获取servlet请求中的参数,并传递给跳转的JSP页面?例如访问http://localhost:8088/bbs ...

  5. 在jsp页面如何获取servlet请求中的参数的办法

    在JAVA WEB应用中,如何获取servlet请求中的参数 ,并传递给跳转的JSP页面?例如访问http://localhost:8088/bbs?id=1 当执行这个bbs servlet时,将u ...

  6. 使用map方式获取iris请求中的json请求数据

    获取iris网络请求中 post请求的json数据 官方用法是使用结构体去接收 每次都要对应一个结构体 比较麻烦 本方案是使用map接收 /** * @Description: 从请求中获取参数 * ...

  7. Case Study: 利用PHP获取关系型数据库中多张数据表的数据

    一.目标 该笔记的目的是引导读者借助WampServer平台和MySQL数据库,利用HTML/CSS/JS/PHP设计一个多数据表关联的网页.在上一个案例(Case Study: 利用JS实现数据库网 ...

  8. python获取post请求中的所有参数_Django从POST reques获取请求参数

    我有一个表单,你需要填写使用一个POST请求,但我希望结果是一个重定向到我的网站上的不同页面.有时我希望用户被重定向到配置文件页,有时重定向到购买页.在 因此,我将重定向URI放入发布的表单页面URL ...

  9. CI如何接受POST请求中的JSON数据

    PHP默认只识别application/x-www.form-urlencoded标准的数据类型 "php://input可以读取没有处理过的POST数据.相较于$HTTP_RAW_POST ...

最新文章

  1. 什么样的人不适合做SEO呢
  2. java 27 - 4 反射之 通过反射获取成员变量并使用
  3. linux中以A开头的函数使用方式历程及详解
  4. PCA的原理及MATLAB实现
  5. 数据结构与算法--再来聊聊数组
  6. 如何在Gradle多项目构建中管理依赖项
  7. 在没有插件的情况下为Chrome设置Proxy
  8. Linux常用指令指南
  9. cmos和ttl_CMOS与TTL电路的详细对比区别
  10. 引领智慧教育,联想云桌面如何打造教育“一朵云”?
  11. 51单片机——独立按键
  12. Jenkins知识地图
  13. html doc全称,html标签全称与功能介绍.doc
  14. Python接口自动化之ddt学习笔记
  15. 黑马畅购商城06-Elasticsearch
  16. 很舒服的几句话,心静,人就不会累了
  17. 台式计算机使用寿命,惠普台式电脑怎么样(深度评测其质量及使用寿命)
  18. 《淘宝网店》:计算总收益
  19. 发送ajax请求接收json数据,ajax接收到的json数据是空的
  20. vCenter学习笔记

热门文章

  1. 学习 Java 8 - 函数式接口 Lambda
  2. windows消息队列。DispatchMessage。 PostMessage
  3. 【剑指offer-Java版】31连续子数组的最大和
  4. Ninja提升编译速度的方法-Android10.0编译系统(十)
  5. window平台下 Eclipse Ndk开发中的Method 'NewStringUTF' could not be resolved问题
  6. python主要学哪些课程_Python学习课程大纲自学Python参考
  7. 如何在Storyboard中使用Scroll view
  8. webpack打包后自动弹出浏览器查看效果
  9. C++ delete 和 delete []的区别
  10. WPF MVVM从入门到精通1:MVVM模式简介