当前位置:网站首页>技术分享 | 接口测试中如何使用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();
}
}
边栏推荐
- JSP out.println()方法具有什么功能呢?
- BGP 第一次实验
- Web开发
- 基于数据驱动的变电站巡检机器人自抗扰控制
- JSP out. The write () method has what function?
- 基于注意力机制的多特征融合人脸活体检测
- JS中对作用域链的理解(查找变量)
- After reshipment tencent greetings to monitor if the corresponding service does not exist by sc. Exe command to add services
- 测试用例:四步测试设计法
- Angr(十二)——官方文档(Part3)
猜你喜欢

Redis - message publish and subscribe

交返是做日内交易的必要条件

BGP综合实验 建立对等体、路由反射器、联邦、路由宣告及聚合

go笔记之——goroutine

Automatic conversion of Oracle-style implicit joins to ANSI JOINs using jOOQ

uni-app project summary

go笔记记录——channel

Kunpeng compile and debug plug-in actual combat
![[21-Day Learning Challenge] A small summary of sequential search and binary search](/img/81/7339a33de3b9e3aec0474a15825a53.png)
[21-Day Learning Challenge] A small summary of sequential search and binary search

鲲鹏编译调试插件实战
随机推荐
MLX90640 红外热成像仪测温模块开发笔记(完整版)
Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
什么是低代码(Low-Code)?低代码适用于哪些场景?
如何期货开户和选择期货公司?
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
辨析内存函数memset、memcmp、memmove以及memcpy
NodeJs, all kinds of path
nodeJs--各种路径
MySQL常用语句整理
C language character and string function summary (2)
交返是做日内交易的必要条件
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
2022/08/01 Study Notes (day21) Generics and Enums
JS中的防抖和节流
期货开户是否有资金门槛?
JSP built-in object out object function introduction
含外部储能的电力系统暂态稳定分布式控制
基于超参数自动寻优的工控网络入侵检测
IO stream basics
傅立叶变换相关公式