当前位置:网站首页>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

原网站

版权声明
本文为[scwMason]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/221/202208090757054941.html