当前位置: 首页> 教育> 就业 > 接口测试JSON/XML请求

接口测试JSON/XML请求

时间:2025/7/9 6:57:53来源:https://blog.csdn.net/2301_77783487/article/details/139315524 浏览次数:0次

目录:

  • JSON请求
  • XML请求

JSON简介:

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

JSON请求:

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

构造JSON请求体:

package com.ceshiren.jsonpath;import org.junit.jupiter.api.Test;import static io.restassured.RestAssured.given;public class jsonStrTest {@Testvoid testJsonStr(){//定义请求数据:jsonstrString 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对象:

package com.ceshiren.jsonpath;import org.junit.jupiter.api.Test;import java.util.HashMap;import static io.restassured.RestAssured.given;public class jsonObjTest {@Testvoid 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依赖配置:

 <dependency><groupId>com.jayway.jsonpath</groupId><artifactId>json-path</artifactId><version>${json-path.version}</version></dependency>

xml实例:

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 {@Testvoid 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文件内容:

<Envelop xmlns="http://schemas.xmlsoap.org/soap/envelope/"><Body><Add xmlns="http://tempuri.org/"><intA>1</intA><intB>1</intB></Add></Body>
</Envelop>

关键字:接口测试JSON/XML请求

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: