当前位置:网站首页>技术分享 | 接口测试中如何使用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();
}
}
边栏推荐
- Pytorch seq2seq 模型架构实现英译法任务
- AXI4协议介绍
- 基于注意力机制的多特征融合人脸活体检测
- 460. LFU cache
- 什么是低代码(Low-Code)?低代码适用于哪些场景?
- 不要用jOOQ串联字符串
- Stapler:1 靶机渗透测试-Vulnhub(STAPLER: 1)
- [Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
- MYSQL(基本篇)——一篇文章带你走进MYSQL的奇妙世界
- [HCIP] BGP Small Experiment (Federation, Optimization)
猜你喜欢
随机推荐
Identify memory functions memset, memcmp, memmove, and memcpy
String splitting function strtok exercise
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
Day11 shell脚本基础知识
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
Industrial control network intrusion detection based on automatic optimization of hyperparameters
抖音数据接口API-获取用户主页信息-监控直播开启
如何期货开户和选择期货公司?
What is it like to trade for a living?
Grid false data injection attacks detection based on coding strategy
ROS dynamic parameters
go笔记之——goroutine
请教一下本网站左下角的动漫人物是怎么做的?
js中内存泄漏的几种情况
Routing strategy
C language character and string function summary (2)
管理基础知识21
攻防世界-web-Training-WWW-Robots
业务测试如何避免漏测 ?
期货开户手续费的秘密成了透明









