REST Assured can be used to test the XML web service as well. Now, In this example, we will learn “How to send a POST XML request using Rest Assured to a Rest API endpoint”.

 

Simple representation of POST XML Request using RESTAssured...!!! Share on X

Check out: KARATE DSL (API Testing) TUTORIAL with examples (Latest 2022)

In this tutorial, we are going to cover the below topics:

  1. How to send POST XML request using Rest Assured?
  2. How to validate the Response?

Let’s begin:

1. How to send POST XML request using Rest Assured?

In this tutorial, we will test the sample XML based Web Service which I built for testing purposes. You can simply click on this link and import this Github repository on your local machine and run it as a spring boot application. 

Note: This project is a Spring Boot web application. So, if you are using Eclipse then install the required spring boot support from the Eclipse Marketplace, or if it’s possible then download ‘STS’ (Spring Tool Suite). STS is a flavor of Eclipse inbuilt with Spring Boot support. 

Once, this service is running on your machine, you can follow the rest of this tutorial to test that service endpoint OR build your own XML based service and test that using the code that I am going to share below by changing the few things of the code according to your web service.

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 clientNumber, name and ssn to add a new client.

The Service endpoint URL is http://localhost:8006/addClient

At the above resource URL, we are going to submit data in the form of XML to add a client.

XML Request Body:

 

Now, here is the code to send the POST request (containing request body in XML format) to the above-mentioned REST API Service Endpoint:

 

Let’s try to understand the code:

1. Setting up Base URI

Base URI is the root address of the Resource. And, by this particular line of code, we are specifying to REST assured to use “http://localhost:8006” as the root URL of the service.

2. Specifying the exact resource to look for and make a POST request to that resource

Here, using this code, we are looking to make a POST XML request to an exact resource which is “/addClient” in this case. Hence, the complete Service Endpoint would be “http://localhost:8006/addClient” to which we are sending a POST request with the XML request body. The XML request body is nothing but the details about the new client that we are looking to add. The output of the POST call will be stored in the REST Assured ‘Response’ object.

3. Response validation

a. response.asString() : It displays the response in a string format

b. response.getStatusCode() : This line of code would extract the status code from the response.

c. response.asString().contains(“100 Tom Cruise 124-542-5555”) : This line of code helps to check if the string ‘100 Tom Cruise 124-542-5555’ present in the response or not.

2. How to validate the Response?

I’ve already explained the validation above. Now, Let’s just execute the above Test class (PostXMLRequest) in eclipse and verify the output:

a. Check for the Response i.e ‘Client information saved successfully::.100 Tom Cruise 124-542-5555’

b. Check for the Status Code i.e ‘200’ as intended.

c. Check if the ‘100 Tom Cruise 124-542-5555’ string is present in the response i.e ‘true’ as intended.

Eclipse Console Output:

 

That’s it, it’s that simple to make a POST Request with XML body using REST Assured API: ?

Simple representation of POST XML Request using RESTAssured...!!! Share on X

 

If you like this post, please click like button and share it with others on Twitter. Also, check out my other useful blog posts on Rest Assured:

Other Useful References:

Author

  • Deepak Verma

    Deepak Verma is a Test Automation Consultant and Software development Engineer for more than 10 years. His mission is to help you become an In-demand full stack automation tester. He is also the founder of Techndeck, a blog and online coaching platform dedicated to helping you succeed with all the automation basics to advanced testing automation tricks.

    http://Techndeck.com deepak.rkgit@gmail.com Verma Deepak