使用 XML 内容作为请求主体时,如何防止 REST Assured POST 请求删除 XML 声明标记?
How to prevent REST Assured POST request from dropping XML declaration tag when using an XML content as the request body?
我使用 REST Assured 来测试将 XML 文件上传到数据库。后端代码写的很好,没有错误。我像这样使用 REST Assured 构建请求(.body() 方法来自 REST Assured 的 RequestSpecification class,它扩展了 RequestSender class)。
response = TestSuite.buildRequest()
.contentType("text/xml")
.header("header1")
.accept("application/json")
.body(uploadPayload)
.post(uploadPath)
uploadPayload 是一个带有 XML 声明标签的 XML 文件内容。这是一个示例文件内容,
<?xml version="1.0" encoding="UTF-8" ?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
这个内容是从项目目录下的文件中读取的,这是我用来读取这个文件内容的方法,它读取了整个文件的所有格式。
public static String getFileContent(File file) {
if (file == null) return
String s = ""
try {
FileReader reader = new FileReader(file)
BufferedReader br = new BufferedReader(reader)
String line
while ((line = br.readLine()) != null) {
s = s + line + "\n"
}
return s
} catch (IOException e) {
throw new IOException("Error getting file content from file.", e)
}
}
我做的是把这个获取到的文件内容添加为请求体(上面提到的请求的uploadPath变量)并发送请求。但是当执行测试时,结果中显示的正文没有 XML 声明标记。但是当我打印内容时,标签就在那里。这是正文在测试结果中的显示方式(没有 XML 声明标记)。
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
我需要获取整个请求体(包括声明标签)。但是由于某种原因,在将其发送到请求正文时它被丢弃了。有人可以告诉我为什么以及解决这个问题的方法吗?非常感谢您的反馈。谢谢。
通过对此进行一些研究,我发现 XML 声明标记因请求中使用的内容类型 (text/xml) 而被删除。关于 the difference between application/xml and text/xml 的 link 解释得清楚而简单。
引用 link、
"According to the standard, text/*-MIME types have a us-ascii character set unless otherwise specified in the HTTP headers. This effectively means that any encoding defined in the XML prolog is ignored. This is of course not the expected and desired behavior."
我使用 REST Assured 来测试将 XML 文件上传到数据库。后端代码写的很好,没有错误。我像这样使用 REST Assured 构建请求(.body() 方法来自 REST Assured 的 RequestSpecification class,它扩展了 RequestSender class)。
response = TestSuite.buildRequest()
.contentType("text/xml")
.header("header1")
.accept("application/json")
.body(uploadPayload)
.post(uploadPath)
uploadPayload 是一个带有 XML 声明标签的 XML 文件内容。这是一个示例文件内容,
<?xml version="1.0" encoding="UTF-8" ?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
这个内容是从项目目录下的文件中读取的,这是我用来读取这个文件内容的方法,它读取了整个文件的所有格式。
public static String getFileContent(File file) {
if (file == null) return
String s = ""
try {
FileReader reader = new FileReader(file)
BufferedReader br = new BufferedReader(reader)
String line
while ((line = br.readLine()) != null) {
s = s + line + "\n"
}
return s
} catch (IOException e) {
throw new IOException("Error getting file content from file.", e)
}
}
我做的是把这个获取到的文件内容添加为请求体(上面提到的请求的uploadPath变量)并发送请求。但是当执行测试时,结果中显示的正文没有 XML 声明标记。但是当我打印内容时,标签就在那里。这是正文在测试结果中的显示方式(没有 XML 声明标记)。
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
</CATALOG>
我需要获取整个请求体(包括声明标签)。但是由于某种原因,在将其发送到请求正文时它被丢弃了。有人可以告诉我为什么以及解决这个问题的方法吗?非常感谢您的反馈。谢谢。
通过对此进行一些研究,我发现 XML 声明标记因请求中使用的内容类型 (text/xml) 而被删除。关于 the difference between application/xml and text/xml 的 link 解释得清楚而简单。
引用 link、
"According to the standard, text/*-MIME types have a us-ascii character set unless otherwise specified in the HTTP headers. This effectively means that any encoding defined in the XML prolog is ignored. This is of course not the expected and desired behavior."