当前位置:网站首页>技术分享 | 接口测试中如何使用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步可实现接口自动化测试
边栏推荐
- PostgreSQL 基础--常用命令
- Check if point is inside rectangle
- numpy.unique
- 研发团队数字化转型实践
- UML diagram of soft skills
- cmd command
- Spark Sql之join on and和where
- Flink学习第四天——完成第一个Flink 流批一体案例
- Deep Learning Fundamentals - Numpy-based Recurrent Neural Network (RNN) implementation and backpropagation training
- Special characters & escapes in bat
猜你喜欢
数据增强--学习笔记(图像类,cnn)
还在纠结报表工具的选型么?来看看这个
[Camp Experience Post] 2022 Cybersecurity Summer Camp
2022第六届强网杯部分wp
TCP 可靠吗?为什么?
[LeetCode304周赛] 两道关于基环树的题 6134. 找到离给定两个节点最近的节点,6135. 图中的最长环
E - Integer Sequence Fair
Building a cloud-native DevOps environment
From 0 to 1: Design and R&D Notes of Graphic Voting Mini Program
Leetcode 129求根节点到叶节点数字之和、104二叉树的最大深度、8字符串转换整数(atoi)、82删除排序链表中的重复元素II、204二分查找、94二叉树的中序遍历、144二叉树的前序遍历
随机推荐
一款简洁的文件传输工具
sys_kill系统调用
请问什么是 CICD
Dynamic Scene Deblurring with Parameter Selective Sharing and Nested Skip Connections
numpy.where
软技能之UML图
Oracle database is set to read-only and read-write
Check if point is inside rectangle
Secondary Vocational Network Security Competition B7 Competition Deployment Process
Interpretation of the paper (GSAT) "Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism"
6133. Maximum number of packets
Special characters & escapes in bat
qt-faststart 安装使用
CF1705D Mark and Lightbulbs
PostgreSQL Basics--Common Commands
Getting started with IDEA is enough to read this article
Chapter 19 Tips and Traps: Common Goofs for Novices
excel split text into different rows
Access the selected node in the console
What can be done to make this SQL into a dangerous SQL?