接口自动化之request模块
1、安装
- 方式一、命令行直接
pip install requests - 方式二、PyCharm中,File >> Settings >> Project:Practice >> Python Interpreter,点击+号,搜索requests,点击Install Package。

2、常用的方法
请求方式
requests.get()# 发送GET请求requests.post()# 发送POST请求requests.delete()# 发送DELETE请求requests.put()# 发送PUT请求requests.request()# 核心方法
返回类型
res = requests.request()res.text# 返回字符串数据res.content# 返回字节格式数据res.json()# 返回字典格式数据res.status_code# 返回状态码res.cookies# 返回Cookie信息res.reason# 返回状态信息res.encoding# 返回编码格式res.headers# 返回响应头信息
核心方法-request()
通过查看python的源码,可以看到Get请求和Post请求,实际上都是去调用request()发送请求。
# Get请求
def get(url, params=None, **kwargs):
r"""Sends a GET request.
:param url: URL for the new :class:`Request` object.
:param params: (optional) Dictionary, list of tuples or bytes to send
in the query string for the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return request("get", url, params=params, **kwargs)
# Post请求
def post(url, data=None, json=None, **kwargs):
r"""Sends a POST request.
:param url: URL for the new :class:`Request` object.
:param data: (optional) Dictionary, list of tuples, bytes, or file-like
object to send in the body of the :class:`Request`.
:param json: (optional) json data to send in the body of the :class:`Request`.
:param \*\*kwargs: Optional arguments that ``request`` takes.
:return: :class:`Response <Response>` object
:rtype: requests.Response
"""
return request("post", url, data=data, json=json, **kwargs)
3、接口实战
本文中的接口示例全部来自微信公众开发平台,详细的请直接参考官网
https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
发送GET请求
import requests
# 发送GET请求
url = "https://api.weixin.qq.com/cgi-bin/token"
data = {
"grant_type": "client_credential",
"appid": "wx1369184acdfe****",
"secret": "34b1eb5f6ed2aa8a2ef3333bac63****"
}
res = requests.get(url=url, params=data)
print(res.json())
发送GET请求
import requests
# 发送GET请求
url = "https://api.weixin.qq.com/cgi-bin/token"
data = {
"grant_type": "client_credential",
"appid": "wx1369184acdfe****",
"secret": "34b1eb5f6ed2aa8a2ef3333bac63****"
}
res = requests.get(url=url, params=data)
print(res.json())
access_token = res.json()["access_token"]
print(access_token)
# post请求
url = "https://api.weixin.qq.com/cgi-bin/clear_quota?access_token=" + access_token + ""
data = {
"appid": "wx1369184acdfe****"
}
res = requests.post(url=url, json=data)
print(res.json())
接口自动化之request模块的更多相关文章
- python接口自动化测试(一)-request模块
urllib.request模块是python3针对处理url的. 1. 首先导入: from urllib import request 2. 构造url,构造url的headers信息和传参[re ...
- 接口自动化-Python3+request上传文件,发送multipart/form-data编码
1.安装requests_toolbelt pip install requests-toolbelt 2.发送文件中的数据 from requests_toolbelt import Multi ...
- 接口自动化测试之-requests模块详解
一.requests背景 Requests 继承了urllib2的所有特性.Requests支持HTTP连接保持和连接池,支持使用cookie保持会话,支持文件上传,支持自动确定响应内容的编码,支持国 ...
- RobotFramework下的http接口自动化Set Request Body 关键字的使用
Set Request Body关键字用来设置http 请求时的body 信息,尤其是在post 请求时,经常需要用到这个关键字. 该关键字接收一个参数,[ body ] 示例1:登录博客园(http ...
- RobotFramework下的http接口自动化Set Request Header 关键字的使用
Set Request Header 关键字用来设置http请求时的请求头部信息. 该关键字接收两个参数,[ header_name | header_value ] 示例1:设置http请求时的Re ...
- 接口自动化之request几种常见请求及响应方法
request 的几种常见方法 1.request.get() 发送get请求 2.request.post() 发送post请求 3.request.delete() 发送delete请求 4.re ...
- 接口自动化框架两大神器-正则提取器和Jsonpath提取器
一 接口自动化框架 一 框架结构 二 结构说明 - API 用于封装被测系统的接口(用request模块封装的请求方法) - TestCase 将一个或多个接口封装成测试用例,并使用UnitTest管 ...
- python+request接口自动化框架
python+request接口自动化框架搭建 1.数据准备2.用python获取Excel文件中测试用例数据3.通过requests测试接口4.根据接口返回的code值和Excel对比 但本章只讲整 ...
- 接口自动化框架(Pytest+request+Allure)
前言: 接口自动化是指模拟程序接口层面的自动化,由于接口不易变更,维护成本更小,所以深受各大公司的喜爱. 接口自动化包含2个部分,功能性的接口自动化测试和并发接口自动化测试. 本次文章着重介绍第一种, ...
- 接口自动化框架2-升级版(Pytest+request+Allure)
前言: 接口自动化是指模拟程序接口层面的自动化,由于接口不易变更,维护成本更小,所以深受各大公司的喜爱. 第一版入口:接口自动化框架(Pytest+request+Allure) 本次版本做了一些升级 ...
随机推荐
- vue+mockjs模拟用户登录接口(高仿书旗)
项目demo:http://39.103.131.74:8888/shuqi
- Python爬取网页遇到:selenium.common.exceptions.WebDriverException解决方法
在PyCharm中写好下列程序: 一运行遇到下列报错: selenium.common.exceptions.WebDriverException: Message: 'chromedriver' e ...
- 融合数据库生态:利用 EventBridge 构建 CDC 应用
简介: 近期,EventBridge 事件流已经支持了基于阿里云 DTS服务的 CDC 能力.本文将从 CDC.CDC 在 EventBridge 上的应用以及若干最佳实践场景等方面,为大家介绍如何利 ...
- 性能提升一倍!云原生网关支持 TLS 硬件加速
简介:业界在优化 HTTPS 的性能上也做了诸多探索,传统的软件优化方案有 Session 复用.OCSP Stapling.False Start.dynamic record size.TLS1. ...
- MaxCompute执行引擎核心技术DAG揭秘
简介: 作为业界少有的EB级数据分布式平台,MaxCompute每天支撑上千万个分布式作业的运行.这些作业特点各异,既有包含数十万计算节点的超大型作业,也有中小规模的分布式作业.不同用户对于不同规模 ...
- 阿里云边缘云全新架构升级,助力CDN操控新体验
简介: 本次升级根据上万企业客户的使用反馈和行业应用特征,从简单开通到个性化定制,从内容分发到边缘计算完整解决方案,对客户侧的使用体验进行了全局梳理和全链路优化,推进边缘云CDN操控革新,并逐步构建 ...
- [FAQ] VScode 用户代码片段设置
以PHP为示例,配置如下: { "dpe": { "prefix": "dpe", "body": [ " ...
- pandas:时间序列数据的周期转换
时间序列数据是数据分析中经常遇到的类型,为了更多的挖掘出数据内部的信息,我们常常依据原始数据中的时间周期,将其转换成不同跨度的周期,然后再看数据是否会在新的周期上产生新的特性. 下面以模拟的K线数据为 ...
- linux文本三剑客之awk详解
linux文本三剑客之awk详解 目录 linux文本三剑客之awk详解 1.awk命令详解 1.1 awk的处理流程 1.2 awk中的变量 1.2.1 内置变量 1.2.2 自定义变量 1.3 a ...
- 更新package.json里所有模块
安装该插件 cnpm install -g npm-check-updates 或者 npm install -g npm-check-updates 在有package.json的目录执行 npm- ...