国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁(yè)技術(shù)文章
文章詳情頁(yè)

Python requests接口測(cè)試實(shí)現(xiàn)代碼

瀏覽:3日期:2022-07-11 18:56:12

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)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 项城市| 翁牛特旗| 于都县| 忻州市| 冀州市| 泰宁县| 临武县| 唐海县| 如皋市| 象山县| 玛曲县| 平远县| 乐陵市| 莎车县| 资溪县| 文山县| 旌德县| 光山县| 阜新| 唐山市| 九寨沟县| 海安县| 河曲县| 甘孜| 甘肃省| 西吉县| 乌鲁木齐县| 来宾市| 武邑县| 宁陵县| 兰溪市| 彰化市| 宁夏| 万宁市| 兴安盟| 会东县| 耿马| 黄骅市| 闽清县| 阜新市| 信阳市|