黑松山资源网 Design By www.paidiu.com
1、get方法请求接口
url:显而易见,就是接口的地址url啦
headers:请求头,例如:content-type = application/x-www-form-urlencoded
params:用于传递测试接口所要用的参数,这里我们用python中的字典形式(key:value)进行参数的传递。
举个例子:
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的区别
- requests 模块发送请求有 data、json、params 三种携带参数的方法。
- params 在 get 请求中使用,data、json 在 post 请求中使用
- 常见的 form 表单可以直接使用 data 参数进行报文提交,data 的对象则是 python 中的字典类型
- 如果数据是 json 格式的参数,可直接使用 json 参数进行报文提交
4、接口的返回值操作
text:获取接口返回值的文本格式
json():获取接口返回值的json()格式
status_code:返回状态码(成功为:200)
headers:返回完整的响应头信息(headers['name']:返回指定的headers内容)
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)
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
黑松山资源网 Design By www.paidiu.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
黑松山资源网 Design By www.paidiu.com
暂无评论...
RTX 5090要首发 性能要翻倍!三星展示GDDR7显存
三星在GTC上展示了专为下一代游戏GPU设计的GDDR7内存。
首次推出的GDDR7内存模块密度为16GB,每个模块容量为2GB。其速度预设为32 Gbps(PAM3),但也可以降至28 Gbps,以提高产量和初始阶段的整体性能和成本效益。
据三星表示,GDDR7内存的能效将提高20%,同时工作电压仅为1.1V,低于标准的1.2V。通过采用更新的封装材料和优化的电路设计,使得在高速运行时的发热量降低,GDDR7的热阻比GDDR6降低了70%。
更新日志
2024年10月05日
2024年10月05日
- 群星《前途海量 电影原声专辑》[FLAC/分轨][227.78MB]
- 张信哲.1992-知道新曲与精丫巨石】【WAV+CUE】
- 王翠玲.1995-ANGEL【新艺宝】【WAV+CUE】
- 景冈山.1996-我的眼里只有你【大地唱片】【WAV+CUE】
- 群星《八戒 电影原声带》[320K/MP3][188.97MB]
- 群星《我的阿勒泰 影视原声带》[320K/MP3][139.47MB]
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[320K/MP3][148.91MB]
- 刘雅丽.2001-丽花皇后·EMI精选王【EMI百代】【FLAC分轨】
- 齐秦.1994-黄金十年1981-1990CHINA.TOUR.LIVE精丫上华】【WAV+CUE】
- 群星.2008-本色·百代音乐人创作专辑【EMI百代】【WAV+CUE】
- 群星.2001-同步过冬AVCD【环球】【WAV+CUE】
- 群星.2020-同步过冬2020冀待晴空【环球】【WAV+CUE】
- 沈雁.1986-四季(2012梦田复刻版)【白云唱片】【WAV+CUE】
- 纪钧瀚《胎教古典音乐 钢琴与大提琴的沉浸时光》[FLAC/分轨][257.88MB]
- 《国语老歌 怀旧篇 3CD》[WAV/分轨][1.6GB]