1、POST和GET的区别与选择

GET and POST are two different types of HTTP requests.

According to Wikipedia:

GET requests a representation of the specified resource. Note that GET should not be used for operations that cause side-effects, such as using it for taking actions in web applications. One reason for this is that GET may be used arbitrarily by robots or crawlers, which should not need to consider the side effects that a request should cause.

and

POST submits data to be processed (e.g., from an HTML form) to the identified resource. The data is included in the body of the request. This may result in the creation of a new resource or the updates of existing resources or both.

So essentially GET is used to retrieve remote data, and POST is used to insert/update remote data.

2、伪代码框架:

from flask import Flask
from flask import requestapp = Flask(__name__)@app.route('/users/<user_id>', methods = ['GET', 'POST', 'DELETE'])
def user(user_id):if request.method == 'GET':"""return the information for <user_id>"""...if request.method == 'POST':"""modify/update the information for <user_id>"""# you can use <user_id>, which is a str but could# changed to be int or whatever you want, along# with your lxml knowledge to make the required# changesdata = request.form # a multidict containing POST data...if request.method == 'DELETE':"""delete user with ID <user_id>"""...else:# POST Error 405 Method Not Allowed...

3、如何接收各种类型参数?

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
  • request.json: parsed JSON data. The request must have the application/json content type, or use request.get_json(force=True) to ignore the content type.

All of these are MultiDict instances (except for json). You can access values using:

  • request.form['name']: use indexing if you know the key exists
  • request.form.get('name'): use get if the key might not exist
  • request.form.getlist('name'): use getlist if the key is sent multiple times and you want a list of values. get only returns the first value.

参考链接:

python - Flask example with POST - Stack Overflowhttps://stackoverflow.com/questions/22947905/flask-example-with-post python - Get the data received in a Flask request - Stack Overflowhttps://stackoverflow.com/questions/10434599/get-the-data-received-in-a-flask-request

Flask实现REST API之接收POST和GET请求相关推荐

  1. 通过带Flask的REST API在Python中部署PyTorch

    通过带Flask的REST API在Python中部署PyTorch 在本文中,将使用Flask来部署PyTorch模型,并用讲解用于模型推断的 REST API.特别是,将部署一个预训练的Dense ...

  2. 应用服务器与WSGI协议以及flask后端框架总结(后端接收请求返回响应的整个流程)...

    上次遗留了两个问题,先说一下自己的看法 问题: 1.明明一个线程只能处理一个请求,那么栈里的元素永远是在栈顶,那为什么需要用栈这个结构?用普通变量不行吗. 2._request_ctx_stack和_ ...

  3. java 调用webapi json_java通过url调用web api并接收其返回的json

    java通过url调用webapi并接收其返回的json数据,但现在结果总是:{"result":4,"data":{}}(未认证:),帮助文档如下:API使用 ...

  4. JAVA中调用阿里云语音通知Api并接收消息回执

    JAVA中调用阿里云语音通知Api并接收消息回执 配置文件 需要的包 <dependency><groupId>com.aliyun</groupId><ar ...

  5. vue安装Postcss_Flask和Vue.js构建全栈单页面web应用【通过Flask开发RESTful API】

    前言: 看了一些国外的关于介绍flask和vue的前后端分离的文章,但没看到比较通俗易懂,代码完善的,直到昨天看到一篇新出的文章,而且内容非常棒,所以翻译过来,供大家一起学习. 原文来自Develop ...

  6. golang接收 post和get请求参数处理

    golang接收 post和get请求 1.golang中获取请求接口中数据(GET) 方式一: API参数 ctx.Param(name string)或者ctx.Params.ByName(nam ...

  7. Java 技术篇 - ServerSocket接收http的url请求中包含中文的处理方法,URLDecode与URLEncode,url解码与编码

    效果图: 正常接收到中文的请求是这个样子的:%E6%AC%A2%E8%BF%8E%E6%9D%A5%E5%88%B0%E5%B0%8F%E8%93%9D%E6%9E%A3%E7%9A%84%E5%8D ...

  8. Jira Servcie Desk使用教程(十四):如何接收来自电子邮件的请求?

        JIRA Servcie Desk是一款服务台管理软件,它能够为你的客户提供简单直观的用户体验,并集成了强大的SLA支持.可自定义的服务队列.自动化的请求管理以及实时的报表. 本文将会介绍如何 ...

  9. 内存泄漏的3个解决方案与原理实现,掌握一个轻松应对开发丨内存池|mtrace||API实现|框架封装|中间件|异步请求|连接池

    内存泄漏的3个解决方案与原理实现,掌握一个轻松应对开发 视频讲解如下,点击观看: 内存泄漏的3个解决方案与原理实现,掌握一个轻松应对开发丨内存池|mtrace||API实现|框架封装|中间件|异步请求 ...

  10. PHP接收GET和POST请求

    最近在做一个iOS的Demo的时候,需要一个GET和POST接口来测试,所以找了相关资料写出了以下PHP脚本.因为是以前写的了,相关参考资料无法列出.这个脚本主要的功能是接收GET和POST请求,并实 ...

最新文章

  1. MO-LTR:基于单目RGB视频的多目标定位、跟踪与重建
  2. redis企业级应用(下)-如何维护redis的key
  3. 深入探究Spark -- 了解Executor和参数配置
  4. Mysql基于GTIDs的复制
  5. 【BZOJ-2938】病毒 Trie图 + 拓扑排序
  6. 微软官方pe工具_微软官方下载工具
  7. Linux下查看txt文档
  8. 最强盘点!国产数据分析BI工具怎么选?看这篇就够了
  9. 第一百一十八天 how can I 坚持
  10. 初学者的SDN学习之路
  11. 微信公众号小程序微商城源码
  12. python 泰森多边形边界_geotools中泰森多边形的生成
  13. ARM DS-5 Development Studio 5.28.1 破解激活教程
  14. 连载:面向对象葵花宝典:思想、技巧与实践(27) - 动态模型设计
  15. 虚拟机和linux有啥关系,linux有什么虚拟机
  16. 「杂谈」什么是我心目中深度学习算法工程师的标准
  17. 【ceph相关】ceph常见问题处理
  18. NOIP2004P4 火星人 题解
  19. ZOJ - 1203 Swordfish(最短路)
  20. arduino uno + tb6600 + 42步进电机 自制自动绕线机

热门文章

  1. Spring.net 控制反转(IoC)容器_(收集)
  2. WinServer2003秘笈放送
  3. 06_注册时密码加密
  4. 自动装配的几种方式——Spring IOC/DI(四)
  5. 敏捷开发之极限编程过程
  6. Stylus Loader has been initialized using an options object that does not match the API schema.
  7. Ajax学习笔记-运行原理及实现-2
  8. NodeJS对mysql数据库的简单操作
  9. HashMap,TreeMap,Hashtable,LinkedHashMap的区别
  10. 一台计算机要两个内网,局域网如何在一台电脑上设置两个IP地址