源码如下:
author = 'damao'
import requests
import json
"""查看requests库的官方文档"""
help(requests)
"""发送一个只有data和url的post请求"""
payload = {'username':'帅damao','password':'123456'}
url = "http://httpbin.org/post"
r = requests.post(url=url,data=json.dumps(payload))
res = json.dumps(json.loads(r.text),ensure_ascii=False,sort_keys=True,indent=4,separators=(',',':'))
print(res + "\n\n\n")
"""发送一个带header、data、url的post请求"""
url = "http://172.18.90.8:8381/ZCAppcommon/admin/toLogin.do"
headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134",
"Content-Type":"application/x-www-form-urlencoded; charset=UTF-8",
"X-Requested-With":"XMLHttpRequest",
"Cookie":"JSESSIONID=8A232E729917847D9034DBBE72C7D12B"
}
data = ("loginName=admin&password=12345678&validateCode=1577")
r = requests.post(url=url,headers=headers,data=data) # 在调用https的接口时,可使用verify=False,表示忽略对SSL证书的验证
print(r.text)
实际中的头信息参数、body体参数等可通过Fiddler抓包筛选参数获得;
具体可参见前面Fidder笔记