POST XML Request API Test 

XML type request is an API in the form of an object which is used to interact with servers. The data can be retrieved from the URL without doing the full-page refresh.

It is mostly used in Ajax programming.

Let us assume the URL to make an XML Type request is “https://petstore.swagger.io/v2/pet/1”

A brief description of the web service that we are going to test:

1. Service Type : XML

2. Request Endpoint: POST

3. Sending client information like PetID, quantity, ship date, status, etc. to add new pet details.

 

1. Set up a request with the specified base URI

  RestAssured.baseURI = "https://petstore.swagger.io/v2/store";

Base URI is the root address of the Resource. Here we are specifying to REST assured to use the above as the base URI of the service.

 

2. Specify request body in XML format

String requestBody = "<order>\r\n" +
"<id>1</id>\r\n" + "<petId>1</petId>\r\n" +
"<quantity>1</quantity>\r\n" +
"<shipDate>122020-07-16T00:00:00.000+0000</shipDate>\r\n" +
"<status>placed</status>\r\n" +
"<complete>true</complete>\r\n" +

"</order>";

Here we are creating an XML type object to pass it into the request body of the post request.

 

3. Specify the exact resource and perform a POST request to it

RestAssured.given()

          contentType(ContentType.XML)

.accept(ContentType.XML)

.body(requestBody)

.when()

.post("/order");


Here we declared sending the content type in XML format. Thus, POST request is sent with the XML body. Here the contentType of the request accepted is ContentType.XML.

 

4. Response Validation

  .then().assertThat().statusCode(200);

Since it is a POST request for which we are making a call, thus the expected status code would be 200. Here, we are validating the response based on the condition where if status code from the response matches with the value 200.

 

Complete Example Code
@Test
public void xmlPostRequest_Test() {

RestAssured.baseURI = "https://petstore.swagger.io/v2/store";

String requestBody = "<order>\r\n" +
"<id>1</id>\r\n" + "<petId>1</petId>\r\n" +
"<quantity>1</quantity>\r\n" +
"<shipDate>122020-07-16T00:00:00.000+0000</shipDate>\r\n" +
"<status>placed</status>\r\n" +
"<complete>true</complete>\r\n" +

"</order>";

RestAssured.given()

          contentType(ContentType.XML)

.accept(ContentType.XML)

.body(requestBody)

.when()

.post("/order")

.then().assertThat().statusCode(200);
}