接口测试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>
相关推荐
菜鸟程序员专写BUG15 小时前
SpringBoot 接口返回异常全集|JSON解析失败/响应乱码/状态码错误完美解决
spring boot·后端·json
xuhaoyu_cpp_java17 小时前
XML学习
xml·java·笔记·学习
张涛酱10745618 小时前
Jackson 严格解析:拒绝"温柔"的 JSON
spring boot·json
nilm6118 小时前
作为前端请使用vue2,elementUI框架 根据后端返回的json 生成表格.
前端·elementui·json
wefly20171 天前
免安装!m3u8live.cn在线 M3U8 播放器,小白也能快速上手
java·开发语言·python·json·php·m3u8·m3u8在线转换
-许平安-2 天前
MCP项目笔记七(插件 calculator)
c++·笔记·json·plugin·mcp
ID_180079054732 天前
淘宝商品详情API的调用频率限制是多少?
大数据·数据库·json
wefly20172 天前
jsontop.cn:一站式 JSON 全能工具集,开发全流程效率神器
前端·javascript·python·django·json·json在线转换
Riu_Peter2 天前
【技术】Maven 配置 settings.xml 轮询下载
xml·java·maven
电商API&Tina2 天前
比价 / 选品专用:京东 + 淘宝 核心接口实战(可直接复制运行)
大数据·数据库·人工智能·python·json·音视频