This is the 19th day of my participation in the August Wenwen Challenge.More challenges in August
Hardware and software Environment
- windows 10 64bits
- Anaconda with python 3.7
- Requests 2.25.0
Introduction to the
Requests is a third-party library used to make standard HTTP requests in Python. It abstracts the complexity behind the request into a nice, simple API so you can focus on interacting with the service and using the data in your application.
The installation
Install using PIP
pip install requests
Copy the code
HTTP Request format
Requests are very simple to use. There are different HTTP requests for different methods, such as GET, POST, delete, and so on
R = requests. Get (URL ='url') r = requests. Post (URL ='url') r = requests requests.delete(url='url') r = requests.head(url='url') r = requests.options(url='url')Copy the code
The sample code
Start by writing a daemon for flask restful tutorials we shared earlier
from flask import Flask, jsonify, request from flask_restful import Api, Resource, reqparse USERS = [ {"name": "zhangsan"}, {"name": "lisi"}, {"name": "wangwu"}, {"name": "zhaoliu"} ] class Users(Resource): def get(self): return jsonify(USERS) def post(self): args = reqparse.RequestParser() \ .add_argument('name', type=str, location='json', required=True, Help =" name cannot be empty ") \.parse_args() if args['name'] not in USERS: users.append ({"name": args['name']}) return jsonify(USERS) def delete(self): USERS = [] return jsonify(USERS) class UserId(Resource): def __init__(self): self.parser = reqparse.RequestParser() self.parser.add_argument('name', type=str) self.parser.add_argument('age', type=int) def get(self, userid): datas = self.parser.parse_args() return jsonify( {"name": USERS[int(userid)].get('name'), "age": datas.get('age')} ) def post(self, userid): file = request.files['file'] file.save('flask_file.txt') return jsonify({ 'msg' : 'success' }) app = Flask(__name__) api = Api(app, default_mediatype="application/json") api.add_resource(Users, '/users') api.add_resource(UserId, '/user/< UserId >') app.run(host='0.0.0.0', port=5000, use_reloader=True, debug=True)Copy the code
When complete, start the Flask service
Get request example
Let’s look at a get request with no parameters
The import requests r = requests. Get (' http://127.0.0.1:5000/users') print (r.j son ()) print (r.s tatus_code)Copy the code
The result is as follows
Let’s look at a get request with parameters
The import requests param = {" name ":" lisi ", "age" : "18"} r = requests. Get (' http://127.0.0.1:5000/user/1 ', params=param) print(r.json()) print(r.status_code)Copy the code
The result is as follows
Post request Example
Consider the POST request, which carries JSON data
import requests import json param = {"name" : "xgx"} headers = {"Content-type": "Application/json"} r = requests. Post (' http://127.0.0.1:5000/users', data = json. The dumps (param), headers=headers) print(r.json()) print(r.status_code)Copy the code
The result is as follows
Look again at an example of submitting a file on a POST request
import requests files = {'file': Open (' test. TXT ', 'rb')} r = requests. Post (' http://127.0.0.1:5000/user/1 ', files=files) print(r.json()) print(r.status_code)Copy the code
The result is as follows
Delete request example
Finally, look at the delete request example
The import requests r = requests. Delete (' http://127.0.0.1:5000/users') print (r.j son ()) print (r.s tatus_code)Copy the code
The result is as follows
Python utility module topics
For more useful Python modules, go
Xugaoxiang.com/category/py…
The resources
- requests.readthedocs.io/en/master/
- Flask tutorial