Skip to content

接口请求体-xml

接口请求体 - XML

简介

在做接口自动化的时候可能会遇到一种接口,它的参数数据是通过 xml 进行传递的。

post 请求相对于 get 请求多一个 body 部分,body 部分常见的数据类型有以下四种(注意是常见的,并不是只有4种)

  • application/x-www-form-urlencoded
  • application/json
  • text/xml
  • multipart/form-data

其中 XML 是可扩展标记语言,是用于描述数据、存储数据、传输(交换)数据,与 HTML 类似,XML 使用标签来标识数据的结构和内容,用户可以定义自己需要的标记。

应用场景

当进行数据交换、信息配置的时候就可以使用 xml 来进行传输

text/xml的数据类型

  • 首先要确定 post 请求的 body 部分类型是 XML 格式:
<?xml version="1.0" encoding = "UTF-8"?>
<COM>
<REQ name="北京-hogwarts">
<USER_ID>bjhongge</USER_ID>
<COMMODITY_ID>123456</COMMODITY_ID>
<SESSION_ID>abcdefg123</SESSION_ID>
</REQ>
</COM>

XML 请求

支持发送 XML 格式请求体。

Python 代码示例

import requests
import pprint
xml = """<?xml version=“1.0” encoding = “UTF-8”?>
<COM>
<REQ name="北京-hogwarts">
<USER_ID>bjhogwarts</USER_ID>
<COMMODITY_ID>123456</COMMODITY_ID>
<SESSION_ID>abcdefg123</SESSION_ID>
</REQ>
</COM>"""

headers = {'Content-Type': 'application/xml'}
# 遇到编码报错时候,对body进行encode
r = requests.post('https://httpbin.ceshiren.com/post', data=xml.encode('utf-8'), headers=headers)
pprint.pprint(r.json())

Java 代码示例

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

import java.io.IOException;

import static io.restassured.RestAssured.given;

public class TestReq {
    String xml = """
            <?xml version="1.0" encoding = "UTF-8"?>
            <COM>
                <REQ name="北京-hogwarts">
                    <USER_ID>bjhogwarts</USER_ID>
                    <COMMODITY_ID>123456</COMMODITY_ID>
                    <SESSION_ID>abcdefg123</SESSION_ID>
                </REQ>
            </COM>
            """;

    @Test
    void testXML() throws IOException {
        String reqBody = IOUtils.toString(xml.getBytes(), "UTF-8");

        given()
                .contentType("application/xml")  // 定制请求内容媒体类型
                .body(reqBody)  // 定制请求体数据
                .when()
                .post("https://httpbin.ceshiren.com/post")  // 发送请求
                .then()
                .log().body(); // 打印响应体信息
    }

}

从文件中读取XML数据

  • xml格式的数据写到代码里面,不太直观,后期维护也不方便,可以把xml格式数据单独拿出来写到一个文件里,再用open函数去读取

  • 新建一个xml_xml文件,写入内容如下:

  • Python: 用open函数去读xml内容:

import requests
import pprint
with open('xml_xml.xml', encoding='utf-8')as fp:
    xml=fp.read()

headers = {'Content-Type': 'application/xml'}
# 遇到编码报错时候,对body进行encode
r = requests.post('https://httpbin.ceshiren.com/post', data=xml.encode('utf-8'), headers=headers)
pprint.pprint(r.json())
- Java: 用IOUtils工具类读取xml内容:

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 TestReq {
    @Test
    void testXML() throws IOException {
        // 定义请求体数据:源自文件对象
        File file = new File("src/test/resources/xml_xml.xml");
        FileInputStream fis = new FileInputStream(file);
        String reqBody = IOUtils.toString(fis, "UTF-8");

        given()
                .contentType("application/xml")  // 定制请求内容媒体类型
                .body(reqBody)  // 定制请求体数据
                .when()
                .post("https://httpbin.ceshiren.com/post")  // 发送请求
                .then()
                .log().body(); // 打印响应体信息
    }
}
  • 打印结果

总结

  • 简介
  • text/xml的数据类型
  • XML 请求
  • 从文件中读取XML数据