当前位置:网站首页>技术分享 | 接口测试中如何使用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();
}
}
边栏推荐
猜你喜欢
随机推荐
基于编码策略的电网假数据注入攻击检测
DFS详解
路由策略
go笔记记录——channel
不要用jOOQ串联字符串
Detailed explanation of JSP request object function
Industrial control network intrusion detection based on automatic optimization of hyperparameters
Microsoft PC Manager V2.1 beta version officially released
TCL: Pin Constraints Using the tcl Scripting Language in Quartus
Knowing the inorder traversal of the array and the preorder traversal of the array, return the postorder history array
C语言实现扫雷游戏
PowerBI商学院佐罗BI真经连续剧
Quick solution for infix to suffix and prefix expressions
JS中的防抖和节流
Kotlin协程:创建、启动、挂起、恢复
ES6对箭头函数的理解
Unknown CMake command “add_action_files“
面试:简单介绍你参与的一个项目
管理基础知识20
Stapler:1 靶机渗透测试-Vulnhub(STAPLER: 1)









