接口测试JSON/XML请求

目录:

  • JSON请求
  • XML请求

JSON简介:

  • 是JavaScript Object Notation的缩写
  • 是一种轻量级的数据交换格式
  • 是理想的接口数据交换语言

JSON请求:

  • 构造JSON请求体
  • JSON字符串
  • HashMap对象+Jackson库

构造JSON请求体:

java 复制代码
package com.ceshiren.jsonpath;

import org.junit.jupiter.api.Test;

import static io.restassured.RestAssured.given;

public class jsonStrTest {

    @Test
    void testJsonStr(){


        //定义请求数据:jsonstr
        String jsonStr = "{\"hello\":\"hogwarts\"}";

        given()
                //指定内容类型
                .contentType("application/json")
                //传入数据对象
                .body(jsonStr)
                .log().headers()
                .log().body()
        .when()
                .post("https://httpbin.ceshiren.com/post")
        .then()
//                .log().all()
                //简单的断言
                .statusCode(200);


    }
}

使用HashMap对象:

java 复制代码
package com.ceshiren.jsonpath;

import org.junit.jupiter.api.Test;

import java.util.HashMap;

import static io.restassured.RestAssured.given;

public class jsonObjTest {


    @Test
    void JsonObjTest(){

        //定义请求数据体
        HashMap<String,String> jsonObj  =new HashMap<String,String>();
        //填充数据
        jsonObj.put("hello", "hogwarts");


        given()
                //请求体类型
                .contentType("application/json")
                .body(jsonObj)
                .log().headers()
                .log().body()
        .when()
                .post("https://httpbin.ceshiren.com/post")
        .then()
                .statusCode(200);
    }

}

XML简介:

  • 是eXtensible Markup Language的缩写
  • 是可扩展标记语言,类似HTML
  • 是用来传输和存储数据
  • 是通过<>标签来描述信息
  • 是W3C的推荐标准

XML是一个完整的标记语言,而JSON不是

XML请求:

  • 构建XML请求体
    • 外部XML文件
    • 字符串

外部XML文件:

IOUtils依赖配置:

java 复制代码
 <dependency>
      <groupId>com.jayway.jsonpath</groupId>
      <artifactId>json-path</artifactId>
      <version>${json-path.version}</version>
    </dependency>

xml实例:

java 复制代码
package com.ceshiren.jsonpath;

import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;

import static io.restassured.RestAssured.given;

public class xmlTest {


    @Test
    void testxmlTest() throws IOException {
        //定义请求体数据,XML文件
        File file = new File("src/test/resources/add.xml");
        //使用文件输入流,读取信息
        FileInputStream fis = new FileInputStream(file);
        //定义请求体的信息
        String reqBody = IOUtils.toString(fis, "UTF-8");
        given()
                .contentType("text/xml")//设定请求内容媒体类型
                .body(reqBody)//定制请求体数据
                .log().headers()
                .log().body()

        .when()
                .post("http://dneonline.com/calculator.asmx")
        .then()
                .log().all()
                .statusCode(500);//响应断言
    }


}

add.txt文件内容:

java 复制代码
<Envelop xmlns="http://schemas.xmlsoap.org/soap/envelope/">
    <Body>
        <Add xmlns="http://tempuri.org/">
            <intA>1</intA>
            <intB>1</intB>
        </Add>
    </Body>
</Envelop>
相关推荐
DarkLONGLOVE几秒前
一文读懂 XML 文档:概念、结构与应用场景!
前端·json
howard200517 小时前
2.1 掌握XML基础知识
xml·半结构化数据
我是大头鸟17 小时前
Spring 面向切面编程 XML 配置实现
xml·java·spring
Java搬码工17 小时前
Map<String,Object>转换为XML报文的方法
xml·java
hkNaruto17 小时前
【AI】让deepseek_r1 671b将draw.io对应的xml数据格式转换成markdown上可以直接渲染出来的Mermaid格式
xml·ai·draw.io
魔障阿Q17 小时前
labelimg标注的xml标签转换为yolo格式标签
xml·人工智能·python·深度学习·yolo·计算机视觉
Newfocus!1 天前
GPU算力哪家好?GpuGeek推出高性能GPU云服务
json
晓翔仔1 天前
利用 requestrepo 工具验证 XML外部实体注入漏洞
xml·网络安全·信息安全·渗透测试·漏洞
故事里故去1 天前
.net 6.0 webapi支持 xml返回xml json返回json
xml·json·.net
Java陈序员2 天前
IDEA 必备插件!轻松搞定 JSON 格式化!
java·json·intellij idea