当前位置:网站首页>技术分享 | 接口测试中如何使用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();
}
}
边栏推荐
- Mean Consistency Tracking of Time-Varying Reference Inputs for Multi-Agent Systems with Communication Delays
- 期货开户是否有资金门槛?
- Double queue implementation stack?Dual stack implementation queue?
- uni-app project summary
- 管理基础知识19
- 这 4 款电脑记事本软件,得试试
- NodeJs, all kinds of path
- JSP out.write()方法具有什么功能呢?
- Transient Stability Distributed Control of Power System with External Energy Storage
- 期货公司开户实力经纪业务的规模
猜你喜欢
随机推荐
AXI4协议介绍
如何期货开户和选择期货公司?
Day11 shell脚本基础知识
ICML 2022 || 局部增强图神经网络GNN,在 GCN 和 GAT基础上 平均提高了 3.4% 和 1.6%
bgp 聚合 反射器 联邦实验
GIF making - very simple one-click animation tool
已知中序遍历数组和先序遍历数组,返回后序遗历数组
测试用例:四步测试设计法
CRS management and maintenance
C language character and string function summary (2)
How to use the go language standard library fmt package
期货开户如何确定期货公司正规性?
What is the function of the JSP out.println() method?
使用jOOQ将Oracle风格的隐式连接自动转换为ANSI JOIN
不要用jOOQ串联字符串
青蛙跳台阶
好的期货公司开户让人省心省钱
MLX90640 红外热成像仪测温传感器模块开发笔记(十) 成果展示-红眼睛相机
JS中的防抖和节流
JSP out.println()方法具有什么功能呢?