当前位置:网站首页>技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
2022-08-01 23:36:00 【测试界的飘柔】
json 是一种轻量级的传输数据格式,用于数据交互。json 请求类型的请求头中的 Content-Type 对应为 application/json。碰到这种类型的接口,使用 Java 的 REST Assured 或者 Python 的 Requests 均可解决。
实战演示
Python演示代码
在 Python 中,使用 json 关键字参数发送 json 请求并传递请求体信息。
>>> import requests
>>> r = requests.post(
'https://httpbin.ceshiren.com/post',
json = {'key':'value'})
>>> r.request.headers
{'User-Agent': 'python-requests/2.22.0',
'Accept-Encoding': 'gzip, deflate',\
'Accept': '*/*', 'Connection': 'keep-alive',
'Content-Length': '16',\
'Content-Type': 'application/json'}
如果请求的参数选择是json,那么Content-Type自动变为application/json。
Java 演示代码
在 Java 中,使用contentType()方法添加请求头信息,使用body()方法添加请求体信息。
import static org.hamcrest.core.IsEqual.equalTo;
import static io.restassured.RestAssured.*;
public class Requests {
public static void main(String[] args) {
String jsonData = "{\"key\": \"value\"}";
//定义请求头信息的contentType为application/json
given().contentType("application/json").
body(jsonData).
when().
post("https://httpbin.ceshiren.com/post").
then().body("json.key", equalTo("value")).log().all();
}
}
喜欢软件测试的小伙伴们,如果我的博客对你有帮助、如果你喜欢我的博客内容,请 “点赞” “评论” “收藏” 一 键三连哦!
软件测试工程师自学教程:
这才是2022最精细的自动化测试自学教程,我把它刷了无数遍才上岸字节跳动,做到涨薪20K【值得自学软件测试的人刷】
软件测试工程师月薪2W以上薪资必学技能 — Python接口自动化框架封装.
美团面试真题_高级测试25K岗位面试 — 软件测试人都应该看看
软件测试必会_Jmeter大厂实战 — 仅6步可实现接口自动化测试
边栏推荐
- qt-faststart installation and use
- 6132. 使数组中所有元素都等于零-快速排序法
- numpy.hstack
- Sql之各种Join
- UML diagram of soft skills
- 6132. All the elements in the array is equal to zero - quick sort method
- 研发团队数字化转型实践
- The third chapter of the imitation cattle network project: develop the core functions of the community (detailed steps and ideas)
- 检查点是否在矩形内
- problem solved
猜你喜欢
系统可用性:SRE口中的3个9,4个9...到底是个什么东西?
CAKE:一个用于多视图知识图谱补全的可扩展性常识感知框架
cdh6打开oozieWeb页面,Oozie web console is disabled.
cmd command
ICLR 2022最佳论文:基于对比消歧的偏标签学习
邻接表与邻接矩阵
6134. 找到离给定两个节点最近的节点-力扣双百代码
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
D - Linear Probing- 并查集
Spark Sql之join on and和where
随机推荐
Calculate the distance between two points
CF1705D Mark and Lightbulbs
chrome copies the base64 data of an image
研发团队数字化转型实践
Data Organization --- Chapter 5 Trees and Binary Trees --- The Concept of Binary Trees --- Application Questions
Getting started with IDEA is enough to read this article
What is CICD excuse me
软件测试之移动APP安全测试简析,北京第三方软件检测机构分享
对于在新标签页中打开的链接,始终使用“noopener”或“noreferrer”
Is TCP reliable?Why?
颜色透明参数
System availability: 3 9s, 4 9s in SRE's mouth... What is it?
YOLO等目标检测模型的非极大值抑制NMS和评价指标(Acc, Precision, Recall, AP, mAP, RoI)、YOLOv5中[email protected]与
Oracle database is set to read-only and read-write
中职网络安全竞赛B7比赛部署流程
Chapter 12 End-User Task As Shell Scripts
计算两点之间的距离
从0到1:图文投票小程序设计与研发笔记
2022/7/31
How to better understand and do a good job?