Simulate sending a post request
The basics of sending post requests dumps and loads
Code example:
# Send post request import requests,json # Basic knowledge of sending post request dumps and loads str_dict ={'name':'xiaoming','age':'20','sex':'male'} print(type(str_dict )) str1 = json.dumps(str_dict) # 1, json.dumps converts dictionaries and json objects into strings print(type(str1)) print(str1) str2 = '{"name":"tom"," age":"22","sex":"male"}' # Note that this is a string, and double quotes must be used in it str_json = json.loads(str2) # 2, json.loads is to convert the string into a dictionary, json object print(type(str_json)) print(str_json['name'],str_json.get('age'))
Take the WeChat open platform as an example
send post request
# 1. Get token url = 'https://api.weixin.qq.com/cgi-bin/token' data = {'grant_type':'client_credential', 'appid':'wxf14419077f707', 'secret':' 92a113bd4b5ffdc72144740dc7123'} response = requests.get(url=url,params=data) # The response is str type, so we need to convert the response into json json_obj = response.json() token = json_obj['access_token'] print(token) # 2, Create a new user tag tag_url = 'https://api.weixin.qq.com/cgi-bin/tags/create' tag_data = {'access_token':token} tag_json_body = {"tag":{"name" : "Changsha 01"} } headers = {"content-type":"application/json"} # Sending json data must have header information content-type # The parameters in the body in the post request are passed through data and json # If the data in the body is in json format, you can directly use the json=body value when sending # response = requests.post(url=tag_url,params=tag_data,headers=headers,json=tag_json_body) # If the data in the body is in json format, use data=json.dumps(body value) when sending response = requests. post(url=tag_url,params=tag_data,headers=headers,data=json.dumps(tag_json_body)) print(response.content.decode("utf-8"))
View execution results:
upload files
import requests # 1, get token url = 'https://api.weixin.qq.com/cgi-bin/token' data = {'grant_type':'client_credential', 'appid':'wxf14419077f707856', 'secret' :'92a113bd4b5ffdc72144740dc7123c99'} response = requests.get(url=url,params=data) # The response is str type, so we need to convert the response into json json_obj = response.json() token = json_obj['access_token'] print( token) # upload file wx_url = "https://api.weixin.qq.com/cgi-bin/media/upload" wx_data = {"access_token":token,"type":"image"} file = {"files ":open("E:/12345.png","rb")} # Note: you must use a dictionary open res = requests.post(url=wx_url,params=wx_data,files=file) print(res.content.decode("utf-8"))
View execution results
Encapsulate post request
Code example:
# 封装post方法 def send_post(url,data,json_info): headers = {"content-type": "application/json"} response = requests.post(url=url,params=data,json=json_info,headers=headers) return response print(send_post(url=tag_url,data=tag_data,json_info=tag_json).content.decode("utf-8"))
Encapsulate the main method
Code example:
# 封装main方法 def run_main(method,url,data=None,json_info=None): response = None if method == "GET": response = send_get(url,data) elif method == "POST": response = send_post(url,data,json_info) else: print("参数错误") response = None return response print(run_main("GET","https://www.jd.com/").content.decode("utf-8"))
Encapsulation test class
Sample code:
# 将写好的get、post、run_mian方法做成类 import requests class run_test: session_obj = requests.session() def __init__(self,method,url,params=None,data=None,headers=None): self.method = method self.url = url self.params = params self.data = data self.headers = headers def send_get(self): res = run_test.session_obj.get(url=self.url,params=self.params,headers=self.headers) return res def send_post(self): res = run_test.session_obj.post(url=self.url,params=self.params, data=self.data,headers=self.headers) return res def run_main(self): if self.method == "GET": res = self.send_get() elif self.method == "POST": res = self.send_post() else: print("请求方式错误,请检查!") res = None return res if __name__ == "__main__": method = "GET" url = "https://www.jd.com" headers = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"} test_obj = run_test(method=method,url=url,headers=headers) response = test_obj.run_main() print(response.content.decode("utf-8"))
Practical case
Optical theory is useless, you have to learn to follow along, and you have to do it yourself, so that you can apply what you have learned to practice. At this time, you can learn from some actual combat cases.
If it is helpful to you, please like and collect it to give the author an encouragement. It is also convenient for you to quickly find it next time.
If you don’t understand, please consult the small card below. The blogger also hopes to learn and progress with like-minded testers
At the right age, choose the right position, and try to give full play to your own advantages.
My road of automated test development is inseparable from the plan of each stage along the way, because I like planning and summarizing,
Test and develop video tutorials, study notes and receive portals! ! !