How to send complex XML using the rest of the information

Using rest-sure, we can easily execute GET, POST, and other methods. In the example below, we send 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 a POST request with a 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

+5
source share
2 answers

I keep my bodies in the resource 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) 
+2
source

I believe you can just do this:

 given(). contentType("application/xml"). body(yourbody). ... ... 

You can also send serializable objects: https://github.com/jayway/rest-assured/wiki/Usage#serialization

0
source

Source: https://habr.com/ru/post/1242379/


All Articles