Using rest-assured we can easily perform GET, POST and other methods. In the example below we are sending a POST to an API that returns a JSON response.
@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"));
}
But I need to create POST request with complex XML body. Body example:
<?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>
How can I do this? Thank you in advance
I keep my bodies in the resources directory, and read them into a string using the following method:
public static String generateStringFromResource(String path) throws IOException {
return new String(Files.readAllBytes(Paths.get(path)));
}
Then in my request I can say
String myRequest = generateStringFromResource("path/to/xml.xml")
given()
.contentType("application/xml")
.body(myRequest)
.when()
.put("my.url/endpoint/")
.then()
statusCode(200)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With