[英]How to pass large and complex xml body in rest assured framework using java
[英]How to post complex XML using rest assured
使用放心的我们可以轻松地执行GET,POST和其他方法。 在下面的示例中,我们将POST发送到返回JSON响应的API。
@Test
public void reserveARide()
{
given().
header("Authorization", "abcdefgh-123456").
param("rideId", "gffgr-3423-gsdgh").
param("guestCount", 2).
when().
post("http://someWebsite/reserveRide").
then().
contentType(ContentType.JSON).
body("result.message", equalTo("success"));
}
但是我需要使用复杂的XML主体创建POST请求。 正文示例:
<?xml version="1.0" encoding="UTF-8"?>
<request protocol="3.0" version="xxx" session="xxx">
<info1 param1="xxx" version="xxx" size="xxx" notes="xxx"/>
<info2 param1="xxx" version="xxx" size="xxx" notes="xxx"/>
</request>
我怎样才能做到这一点? 先感谢您
我将主体保留在资源目录中,并使用以下方法将其读取为字符串:
public static String generateStringFromResource(String path) throws IOException {
return new String(Files.readAllBytes(Paths.get(path)));
}
然后我可以说
String myRequest = generateStringFromResource("path/to/xml.xml")
given()
.contentType("application/xml")
.body(myRequest)
.when()
.put("my.url/endpoint/")
.then()
statusCode(200)
我相信您可以简单地做到这一点:
given().
contentType("application/xml").
body(yourbody).
...
...
您还可以发送可序列化的对象,请参见: https : //github.com/jayway/rest-assured/wiki/Usage#serialization
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.