Python requests接口測試實現(xiàn)代碼
1、get方法請求接口
url:顯而易見,就是接口的地址url啦
headers:請求頭,例如:content-type = application/x-www-form-urlencoded
params:用于傳遞測試接口所要用的參數(shù),這里我們用python中的字典形式(key:value)進(jìn)行參數(shù)的傳遞。
舉個例子:
import requests
url="http://api.shein.com/login"
header={"content-type":"application/x-www-form-urlencoded"}
param={"user_id":123456,"email":"123456@163.com"}
timeout=0.5
response = 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方法請求接口
import requests
url="http://api.shein.com/login"
header={"content-type":"application/x-www-form-urlencoded"}
param={"user_id":123456,"email":"123456@163.com"}
timeout=0.5
response = requests.post(url, headers=header, data=param, timeout=timeout)
# response = requests.request("post",url,headers=header,data=param,timeout=timeout)
print (response.text)
import requests
url = "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()請求接口
import requests
session = requests.Session() #定義全局session,通過 session 保持會話
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"}
#通過全局 session 請求接口
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"}
# 通過全局 session 請求接口
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ā)送請求有 data、json、params 三種攜帶參數(shù)的方法。
- params 在 get 請求中使用,data、json 在 post 請求中使用
- 常見的 form 表單可以直接使用 data 參數(shù)進(jìn)行報文提交,data 的對象則是 python 中的字典類型
- 如果數(shù)據(jù)是 json 格式的參數(shù),可直接使用 json 參數(shù)進(jìn)行報文提交
4、接口的返回值操作
text:獲取接口返回值的文本格式
json():獲取接口返回值的json()格式
status_code:返回狀態(tài)碼(成功為:200)
headers:返回完整的響應(yīng)頭信息(headers['name']:返回指定的headers內(nèi)容)
encoding:返回字符編碼格式
url:返回接口的完整url地址
import requests
url = "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)
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
python如何統(tǒng)計字符串漢字?jǐn)?shù)量
這篇文章主要介紹了python如何統(tǒng)計字符串漢字?jǐn)?shù)量問題,2024-05-05
Python實現(xiàn)操作Redis所有類型的方法詳解
Redis作為一款高性能的NoSQL數(shù)據(jù)庫,越來越受到了廣大開發(fā)者的喜愛。本篇博客將介紹如何使用Python操作Redis的所有類型,以及一些高級用法,感興趣的可以了解一下2023-04-04
python實現(xiàn)登陸知乎獲得個人收藏并保存為word文件
這篇文章主要介紹了python實現(xiàn)登陸知乎獲得個人收藏并保存為word文件,本文直接給出實現(xiàn)代碼,需要的朋友可以參考下2015-03-03
Python實現(xiàn)城市公交網(wǎng)絡(luò)分析與可視化
這篇文章主要介紹了通過Python爬取城市公交站點、線路及其經(jīng)緯度數(shù)據(jù),并做可視化數(shù)據(jù)分析。文中的示例代碼講解詳細(xì),感興趣的可以學(xué)習(xí)一下2021-12-12
利用Python?Matplotlib繪圖并輸出圖像到文件中的方式
這篇文章主要介紹了利用Python?Matplotlib繪圖并輸出圖像到文件中的方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
使用國內(nèi)鏡像源優(yōu)化pip install下載的方法步驟
在Python開發(fā)中,pip 是一個不可或缺的工具,用于安裝和管理Python包,然而,由于默認(rèn)的PyPI服務(wù)器位于國外,國內(nèi)用戶在安裝依賴時可能會遇到下載速度慢、連接不穩(wěn)定等問題,所以本文將詳細(xì)介紹如何使用國內(nèi)鏡像源來加速pip install -r requirements.txt的過程2025-03-03
win8下python3.4安裝和環(huán)境配置圖文教程
這篇文章主要為大家詳細(xì)介紹了win8下python3.4安裝和環(huán)境配置圖文教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-07-07

