How to send POST XML Request using Rest Assured?

This entry is part 10 of 14 in the series Rest Assured Tutorial

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...!!! Click To Tweet

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

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...!!! Click To Tweet

 

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:

 

Related Posts:

[wcp-carousel id=”5342″]
Series Navigation<< JSON Schema Validation with Rest Assured (Updated 2020)Basic Authentication using Rest Assured >>

5 thoughts on “How to send POST XML Request using Rest Assured?

    1. Hello Deepak, its really a wonderful tutorial !! I was browsing how to parameterize SOAP request and after so many search results, I suddenly got this, which helped me to parameterize. I will try a data driven test now and also SOAP chaining using these parameter methods. You made my day !! Thanks much !!

  1. Hello,

    Thank you for sharing this tutorial, will it be possible to please add instructions on how to start the spring boot server in the local machine. I have never interacted with spring boot and without instructions I am diverging from learning about Rest Assured to learning spring boot.

    Thank You

    1. Thanks Namita.

      Ofcourse, would definitely try to help in setting up the spring boot application on your local machine.

      I hope you already installed STS in your machine and loaded the project from github that I’ve mentioned above which is present at this location “http://github.com/dverma0209/RestService”

      You might be seeing the error on the project that could be because of server is not setup.

      In order to do that, lets download the apache tomcat server from this location https://tomcat.apache.org/download-80.cgi (you can download any version of your choice)

      Once, it’s downloaded, then go back to you STS and right click on the project and click properties.

      A properties window will open.

      On the left panel, look for ‘Target Runtimes’ option.

      Once you choose ‘Target Runtimes’ option, on the right panel, click ‘New’ button.

      On the next window which is ‘New Server Runtime Environment’, under ‘Apache’ folder, choose the appropriate Apache Tomcat version (choose only that option which matches with your downloaded apache version) and click ‘Next’

      On the next window, Under Tomcat Installation Directory field, browse the location of your downloaded apache folder and finish it.

      Now, you are good to go to execute your spring boot application.

      I hope it would clear your doubt on server related issue.

      Kindly let us know if you stuck on anything.

      Thanks

Leave a Reply

Your email address will not be published.Required fields are marked *

Facebook Twitter Instagram YouTube

Apach_HttpClient_Tutorial_Popup_Techndeck