当前位置:网站首页>Different styles of Flask-restful
Different styles of Flask-restful
2022-08-09 08:03:00 【scwMason】
Reference Articlehttp://www.pythondoc.com/Flask-RESTful/quickstart.html
http://www.pythondoc.com/Flask-RESTful/reqparse.html
To summarize this module:
1. You can use add_resource to process urls in batches, no need to add before each [email protected]
2. The most important function of flask-restful is the parameter parsing of requests that carry data in post, which can be roughly understood as:
First you need to create the object:
parser = reqparse.RequestParser()parser.add_argument('task', type=str)
Then parsed parameters are stored in args
args = parser.parse_args()
Parameter location
parser.add_argument('name', type=int, location='form')# Look only in the querystringparser.add_argument('PageSize', type=int, location='args')# From the request headersparser.add_argument('User-Agent', type=str, location='headers')# From http cookiesparser.add_argument('session_id', type=str, location='cookies')# From file uploadsparser.add_argument('picture', type=werkzeug.datastructures.FileStorage, location='files')
Change name
If you don't want to get the parameter value by parameter name, you can:
parser.add_argument('name', type=str, dest='public_name')args = parser.parse_args()args['public_name']
Required parameters
To require an argument by value, just add required=True
to call add_argument()
.
parser.add_argument('name', type=str, required=True,help="Name cannot be blank!")
Cannot get parameters
If we set "session_id" as a cookie, our args will not be available:
parser.add_argument('session_id',type=str,location="cookies")args=parser.parse_args()
Send request:
curl http://localhost:5000/todos -d "task=soming new" -d "name=mason" -d "session_id=hello word" -X POST -v
print args
Multiple values passed
parser.add_argument('session_id',type=str,location="cookies",action="append")
At this time, when passing the value of session_id, you need: -d "session_id=123" -d "session_id=456" like this
These are the points I think are more important in the article, and I summarize them by myself
边栏推荐
猜你喜欢
随机推荐
Introduction to the Endpoint
Anaconda use proxy
记录一次客户的APP数据库版本号升级失败的情况
Selenium测试案例一步步学之(2)Selenium自动测试脚本模块化(下)
测试和开发之间的恩恩怨怨
实现弹簧柔性状态的2种方式 | Solidworks教程
Servlet详解
HOOPS助力 SolidWorks edrawings 引入AR/VR技术
The String class objects created by the JVM memory allocation and the difference between equals and = =
mysql事务(详解)
可能导致Loadrunner检查点中savecount为0的分析
SDRAM的数据存储实现并对其数据进行读写操作
[STL]vector
204. 数素数
Shell之函数与数组
Shell--常用小工具(sort、uniq、tr、cut)
C language: detailed explanation of soda bottle
RAID配置实战
“互联网+”大学生创新创业大赛经历
Forest Program DFS + tanjar cactus