1 目的
构建HTTP请求消息,并且解析收到的HTTP响应消息,根据业务场景来判断是否符合预期。
2 Requests库
用来做收发http请求。
2.1 构建请求
2.2.1 构建请求头
headers = { 'user-agent': 'my-app/0.0.1', 'auth-type': 'jwt-token' }
r = requests.post("http://httpbin.org/post", headers=headers)
2.2.2 构建请求消息体
1)指定代理+get请求
paras={
'key1':'value1',
'key2':'value2'
}
proxies={
'http':'http://127.0.0.1:8888',
'https':'http://127.0.0.1:8888'
}
r=requests.get('url',proxies=proxies,params=paras)
2)post请求,消息体格式可以是xml、json、urlencoded、自定义
A. urlencoded格式
payload = {'key1': 'value1', 'key2': 'value2'}
r = requests.po