Rest-assured4 - JSON数据传参和xml数据传参

JSON简介

JSON是JavaScript Object Notation的简称。

是一种轻量级的数据交换格式。

是理想的接口数据交换语言(前后端数据)

JSON请求

如何构造JSON请求体

方式1:JSON字符串

方式2:HashMap对象 + Jackson库

        

使用JSON字符串传输发送POST请求

这里我们使用POST方法演示,通常只有POST方法可以传输我们的数据体。

步骤: 

1. 定义请求数据体, JSON string数据体的方式

2. 发送post请求

3. 解析结果, 响应断言

public void testJson() {
        //定义请求数据 json string
        String jsonStr = "{\"hello\":\"pig\"}";
        given()
                .contentType("application/json")
                .body(jsonStr)
                .log().headers()
                .log().body()

                .when()
                //发送post请求
                .post("https://httpbin.ceshiren.com/post")

                //断言结果并且打印log
                .then()
//                .log().all()
                //响应状态码200
                .statusCode(200);
    }

 

使用HashMap对象发送POST请求 

步骤:

1.定义请求体数据, HashMap数据对象类型。

2. 发送POST请求。

3. 解析结果,响应断言。

添加JackSon依赖配置 

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.13.0</version>
        </dependency>

代码:

    public void testJsonObj(){
        //定义请求数据, HashMap对象
        HashMap<String, String> jsonObj = new HashMap<>();
        jsonObj.put("hello", "pig");
        given()
                .contentType("application/json")
                .body(jsonObj)
                .log().headers()
                .log().body()

                .when()
                .post("https://httpbin.ceshiren.com/post")


                .then()
                .log().all()
                .statusCode(200);
    }

XML简介 

是eXtensible Markup Language的缩写。

是可拓展标记语言, 类似HTML。

是用来传输和存储数据。

是通过< > 标签来描述信息。

是W3C的推荐标准。

构建XML请求的方式

构建XML请求体:

1. 外部XML文件。

2. 字符串

外部XML文件方式传参

1. 在resorces目录添加xml文件。设定内容请求类型。

2. 发送POST请求。

3. 响应结果,断言。

添加读取文件的依赖,后续需要用。

        <dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>2.11.0</version>
        </dependency>

 

public void testXml(){
        //定义请求体数据 : XML
        File file = new File("src/test/resources/add.xml");
        try {
            FileInputStream fis = new FileInputStream(file);
            String regBody = IOUtils.toString(fis, "UTF8");
            given()
                    .contentType("text/xml") //设定请求内容媒体类型
                    .body(regBody) //定制请求体数据
                    .log().body()

                    .when()
                    .get("http://dneonline.com/calculator.asmx")//发送POST请求

                    .then()
                    .log().all()
                    .statusCode(200);//响应断言

        } catch (IOException e) {
            e.printStackTrace();
        }

当使用`restTemplate.exchange()`方法时,可以通过将请求的HTTP方法、URL、请求头请求体作为参数传递来发送HTTP请求。如果请求需要发送JSON格式的请求体,可以将请求体对象转换为JSON字符串,然后设置请求头中的`Content-Type`为`application/json`。 以下是一个使用`restTemplate.exchange()`方法发送JSON请求体的示例代码: ```java RestTemplate restTemplate = new RestTemplate(); // 创建请求头 HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); // 创建请求体对象 MyRequestBody requestBody = new MyRequestBody("John", "Doe"); // 将请求体对象转换为JSON字符串 ObjectMapper mapper = new ObjectMapper(); String requestBodyJson = mapper.writeValueAsString(requestBody); // 创建请求对象 HttpEntity<String> request = new HttpEntity<>(requestBodyJson, headers); // 发送HTTP请求 String url = "https://example.com/api/users"; ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, request, String.class); ``` 在上面的示例中,`MyRequestBody`是一个包含两个属性的POJO类,表示请求体对象。通过`ObjectMapper`将请求体对象转换为JSON字符串,并将其设置为HTTP请求体。然后,通过设置请求头的`Content-Type`为`application/json`来告诉服务器发送的是JSON格式的请求体。最后,使用`restTemplate.exchange()`方法发送HTTP请求,并将响应体解析为字符串
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值