Python requests接口測(cè)試實(shí)現(xiàn)代碼
1、get方法請(qǐng)求接口
url:顯而易見(jiàn),就是接口的地址url啦
headers:請(qǐng)求頭,例如:content-type = application/x-www-form-urlencoded
params:用于傳遞測(cè)試接口所要用的參數(shù),這里我們用python中的字典形式(key:value)進(jìn)行參數(shù)的傳遞。
舉個(gè)例子:
import requestsurl='http://api.shein.com/login'header={'content-type':'application/x-www-form-urlencoded'}param={'user_id':123456,'email':'123456@163.com'}timeout=0.5response = requests.get(url, headers=header, params=param, timeout=timeout)# response = requests.request('get',url,headers=header,params=body,timeout=timeout)print (response.text)
2、post方法請(qǐng)求接口
import requestsurl='http://api.shein.com/login'header={'content-type':'application/x-www-form-urlencoded'}param={'user_id':123456,'email':'123456@163.com'}timeout=0.5response = requests.post(url, headers=header, data=param, timeout=timeout)# response = requests.request('post',url,headers=header,data=param,timeout=timeout)print (response.text)
import requestsurl = 'https://apipc.xinqgj.com/user/login'payload = {'phone':'17779828887','pwd':'Ty+coun/mUj1saGV2OCK6p5kN9MNt8Uznj'}headers = {’Content-Type’: ’application/json’}response = requests.request('POST', url, headers=headers, json = payload)print(response.text)
3、requests.Session()請(qǐng)求接口
import requestssession = requests.Session() #定義全局session,通過(guò) session 保持會(huì)話class Cms(): def login(self): url = 'http://192.168.1.110:8080/cms/manage/loginJump.do' header = {'Content-Type': 'application/x-www-form-urlencoded'} parmas = {'userAccount': 'admin', 'loginPwd': '123456'} #通過(guò)全局 session 請(qǐng)求接口 res = session.post(url=url, headers=header, data=parmas) print(res.json()) def queryUserList(self): url = 'http://192.168.1.110:8080/cms/manage/queryUserList.do' header = {'Content-Type': 'application/x-www-form-urlencoded'} parmas = {'startCreateDate':'', 'endCreateDate':'', 'searchValue':'', 'page':'1'} # 通過(guò)全局 session 請(qǐng)求接口 res = session.post(url=url, headers=header, data=parmas) print(res.json())if __name__ == ’__main__’: Cms().login() Cms().queryUserList()
注意:Python requests模塊params、data、json的區(qū)別
requests 模塊發(fā)送請(qǐng)求有 data、json、params 三種攜帶參數(shù)的方法。 params 在 get 請(qǐng)求中使用,data、json 在 post 請(qǐng)求中使用 常見(jiàn)的 form 表單可以直接使用 data 參數(shù)進(jìn)行報(bào)文提交,data 的對(duì)象則是 python 中的字典類型 如果數(shù)據(jù)是 json 格式的參數(shù),可直接使用 json 參數(shù)進(jìn)行報(bào)文提交4、接口的返回值操作
text:獲取接口返回值的文本格式
json():獲取接口返回值的json()格式
status_code:返回狀態(tài)碼(成功為:200)
headers:返回完整的響應(yīng)頭信息(headers[’name’]:返回指定的headers內(nèi)容)
encoding:返回字符編碼格式
url:返回接口的完整url地址
import requestsurl = 'https://xxxx.com/user/login'payload = {'phone':'1777982xxxx','pwd':'Ty+coun/mUj1saGV2OCK6p5kN9MNt8UznjaGsQ5A/nKPSH1NZW'}headers = {’Content-Type’: ’application/json’}response = requests.request('POST', url, headers=headers, json = payload)print(response.text)print(response.json())print(response.status_code)print(response.url)print(response.headers)print(response.encoding)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. ASP 信息提示函數(shù)并作返回或者轉(zhuǎn)向2. python matplotlib:plt.scatter() 大小和顏色參數(shù)詳解3. vue使用webSocket更新實(shí)時(shí)天氣的方法4. 淺談python出錯(cuò)時(shí)traceback的解讀5. Python importlib動(dòng)態(tài)導(dǎo)入模塊實(shí)現(xiàn)代碼6. android studio 打包自動(dòng)生成版本號(hào)與日期,apk輸入路徑詳解7. 利用promise及參數(shù)解構(gòu)封裝ajax請(qǐng)求的方法8. 在Android中使用WebSocket實(shí)現(xiàn)消息通信的方法詳解9. Nginx+php配置文件及原理解析10. JSP數(shù)據(jù)交互實(shí)現(xiàn)過(guò)程解析
