Python requests接口测试实现代码


Posted in Python onSeptember 08, 2020

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)

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python和pyqt实现360的CLable控件
Feb 21 Python
Python面向对象编程中关于类和方法的学习笔记
Jun 30 Python
详解Python中使用base64模块来处理base64编码的方法
Jul 01 Python
OpenCV2.3.1+Python2.7.3+Numpy等的配置解析
Jan 05 Python
Python内置模块hashlib、hmac与uuid用法分析
Feb 12 Python
python发送邮件脚本
May 22 Python
解决Python3 抓取微信账单信息问题
Jul 19 Python
详解PyTorch中Tensor的高阶操作
Aug 18 Python
django 中使用DateTime常用的时间查询方式
Dec 03 Python
Python字符串hashlib加密模块使用案例
Mar 10 Python
使用opencv中匹配点对的坐标提取方式
Jun 04 Python
Python实现Appium端口检测与释放的实现
Dec 31 Python
Python unittest装饰器实现原理及代码
Sep 08 #Python
Python selenium环境搭建实现过程解析
Sep 08 #Python
Python unittest生成测试报告过程解析
Sep 08 #Python
Python使用Selenium模拟浏览器自动操作功能
Sep 08 #Python
Python unittest如何生成HTMLTestRunner模块
Sep 08 #Python
Django模型验证器介绍与源码分析
Sep 08 #Python
Python unittest discover批量执行代码实例
Sep 08 #Python
You might like
PHP魔术方法以及关于独立实例与相连实例的全面讲解
2016/10/18 PHP
PHPExcel实现表格导出功能示例【带有多个工作sheet】
2018/06/13 PHP
PHP实现小程序批量通知推送
2018/11/27 PHP
ThinkPHP 5 AJAX跨域请求头设置实现过程解析
2020/10/28 PHP
动态加载图片路径 保持JavaScript控件的相对独立性
2010/09/03 Javascript
js使浏览器窗口最大化实现代码(适用于IE)
2013/08/07 Javascript
用svg制作富有动态的tooltip
2015/07/17 Javascript
JS处理json日期格式化问题
2015/10/01 Javascript
对js中回调函数的一些看法
2016/08/29 Javascript
js实现背景图自适应窗口大小
2017/01/10 Javascript
实例讲解DataTables固定表格宽度(设置横向滚动条)
2017/07/11 Javascript
浅析JavaScript中的平稳退化(graceful degradation)
2017/07/24 Javascript
在vue中解决提示警告 for循环报错的方法
2018/09/28 Javascript
ndm:NPM的桌面GUI应用程序
2018/10/15 Javascript
json 带斜杠时如何解析的实现
2019/08/12 Javascript
解决antd datepicker 获取时间默认少8个小时的问题
2020/10/29 Javascript
微信小程序实现星星评分效果
2020/11/01 Javascript
vue解决跨域问题(推荐)
2020/11/10 Javascript
[00:30]明星选手化身超级英雄!2018DOTA2亚洲邀请赛全明星赛来临!
2018/04/06 DOTA
用Python进行行为驱动开发的入门教程
2015/04/23 Python
Python代码实现http/https代理服务器的脚本
2019/08/12 Python
python扫描线填充算法详解
2020/02/19 Python
HTML5印章绘制电子签章图片(中文英文椭圆章、中文英文椭圆印章)
2019/06/03 HTML / CSS
详解HTML5之pushstate、popstate操作history,无刷新改变当前url
2017/03/15 HTML / CSS
德国家具在线:Fashion For Home
2017/03/11 全球购物
德国电子产品购物网站:TechInTheBasket德国
2018/12/07 全球购物
给排水工程师岗位职责
2013/11/21 职场文书
网络书店创业计划书
2014/02/07 职场文书
奥巴马的演讲稿
2014/05/15 职场文书
2014年发展党员工作总结
2014/11/12 职场文书
长城导游词400字
2015/01/30 职场文书
行政助理岗位职责
2015/02/10 职场文书
学期个人工作总结
2015/02/13 职场文书
交通事故被告答辩状
2015/05/22 职场文书
一文读懂go中semaphore(信号量)源码
2021/04/03 Golang
Python中的 enumerate和zip详情
2022/05/30 Python