当前位置:网站首页>技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
技术分享 | 接口测试中如何使用Json 来进行数据交互 ?
2022-08-02 00:15:00 【叶赫那拉 赫敏】
json 是一种轻量级的传输数据格式,用于数据交互。json 请求类型的请求头中的 Content-Type 对应为 application/json 。碰到这种类型的接口,使用 Java 的 REST Assured 或者 Python 的 Requests 均可解决。
实战演示
在 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 中,使用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();
}
}
边栏推荐
- Multidimensional Correlation Time Series Modeling Method Based on Screening Partial Least Squares Regression of Correlation Variables
- 辨析内存函数memset、memcmp、memmove以及memcpy
- MLX90640 红外热成像仪测温传感器模块开发笔记(十) 成果展示-红眼睛相机
- How does JSP use request to get the real IP of the current visitor?
- 期货开户调整交易所保证金标准
- 21.数据增强
- Async/await principle and execution sequence analysis
- 什么是低代码(Low-Code)?低代码适用于哪些场景?
- poker question
- Angr(十二)——官方文档(Part3)
猜你喜欢

磁盘与文件系统管理

Realize deletion - a specified letter in a string, such as: the string "abcd", delete the "a" letter in it, the remaining "bcd", you can also pass multiple characters to be deleted, and pass "ab" can

Looking back at 5 recessionary times in history: How might this time be different?

管理基础知识21

DFS详解

BGP 第一次实验

期货开户手续费加一分是主流

什么是低代码(Low-Code)?低代码适用于哪些场景?

Double queue implementation stack?Dual stack implementation queue?

C语言函数详解(1)【库函数与自定义函数】
随机推荐
ICML 2022 | GraphFM:通过特征Momentum提升大规模GNN的训练
Redis - message publish and subscribe
如何期货开户和选择期货公司?
Constructor, this keyword, method overloading, local variables and member variables
Arduino Basic Syntax
DOA从一维阵列传感说起
工业信息物理系统攻击检测增强模型
字符串分割函数strtok练习
Don't concatenate strings with jOOQ
磁盘与文件系统管理
Async/await principle and execution sequence analysis
Trie详解
PHP to read data from TXT file
交返是做日内交易的必要条件
els block deformation judgment.
Disk and file system management
Graphical LeetCode - 1161. Maximum Sum of In-Layer Elements (Difficulty: Moderate)
基于数据驱动的变电站巡检机器人自抗扰控制
期货开户手续费的秘密成了透明
Kunpeng compile and debug plug-in actual combat