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:
- How to send POST XML request using Rest Assured?
- 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:
1 2 3 4 5 | <client> <clientNo>100</clientNo> <name>Tom Cruise</name> <ssn>124-542-5555</ssn> </client> |
Now, here is the code to send the POST request (containing request body in XML format) to the above-mentioned REST API Service Endpoint:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import static io.restassured.RestAssured.given; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; public class XMLPostRequest { @Test public void xmlPostRequest_Test() { RestAssured.baseURI = "http://localhost:8006"; String requestBody = "<client>\r\n" + " <clientNo>100</clientNo>\r\n" + " <name>Tom Cruise</name>\r\n" + " <ssn>124-542-5555</ssn>\r\n" + "</client>"; Response response = null; response = given(). contentType(ContentType.XML) .accept(ContentType.XML) .body(requestBody) .when() .post("/addClient"); System.out.println("Post Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); System.out.println("Does Reponse contains '100 Tom Cruise 124-542-5555'? :" + response.asString().contains("100 Tom Cruise 124-542-5555")); } } |
Let’s try to understand the code:
1. Setting up Base URI
1 | RestAssured.baseURI = "http://localhost:8006"; |
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
1 2 3 4 5 6 7 8 | Response response = null; response = given(). contentType(ContentType.XML) .accept(ContentType.XML) .body(requestBody) .when() .post("/addClient"); |
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
1 2 3 | System.out.println("Post Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); System.out.println("Does Reponse contains '100 Tom Cruise 124-542-5555'? :" + response.asString().contains("100 Tom Cruise 124-542-5555")); |
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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | [RemoteTestNG] detected TestNG version 6.14.3 Post Response :Client information saved successfully ::.100 USA 124-542-5555 Status Code :200 Does Reponse contains '100 Tom Cruise 124-542-5555'? :true PASSED: xmlPostRequest_Test =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== =============================================== Default suite Total tests run: 1, Failures: 0, Skips: 0 =============================================== |
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:
Great post, you have pointed out some great points, I besides believe this is a very wonderful website.
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 !!
I am so glad that it helped.
Thank you.
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
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