How to send a POST Request using Rest Assured [2020]

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

Up until now, we have already covered the basics of Rest API Testing using Rest Assured. If you haven’t gone through the basics, please check my previous tutorial. Also, we already covered sending a GET Request using RestAssured. If you haven’t checked that, go for it by clicking this link. Now, in this tutorial, we are going to see “How to send an HTTP POST JSON request using Rest Assured to a Rest API endpoint”.

 

Simple representation of POST JSON Request using REST Assured...!!! Click To Tweet

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

  1. What is HTTP POST Request?
  2. How to send a POST request using Rest Assured?
  3. How to validate the Response?

Let’s begin:

1. What is HTTP POST Request?Http_Post_Request_Rest_Assured_Techndeck

POST is one of the most common methods of HTTP which is used to send data to a server to create/update the resource. Data sent to the server is in the form of either Request Body / Request Parameters which is basically used to create or update the resource on the server.

Some key points of POST requests:

  • POST requests parameters don’t store in the browser history
  • POST requests are used to send data to the server to create or update the resource
  • POST requests cannot be cached
  • POST requests are secure as compared to GET because parameters/data doesn’t store in the browser history
  • POST requests parameter data is unlimited as there are no length restrictions
  • POST requests cannot be bookmarked
  • POST requests allow ASCII characters
  • POST requests are difficult to hack

 

2. How to send a POST request using Rest Assured?

In this tutorial, we will test the ‘Dummy Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purposes which are performing various CRUD operations.

Let’s take an example of one of the API POST endpoint available at the above-mentioned website which is ‘/create’. The full-service URL with the endpoint is ‘http://dummy.restapiexample.com/api/v1/create‘.

At the above resource URL, we are going to submit data in the form of JSON to create an employee.

JSON Request Body:

 

Here is the code to send the POST request (containing request body in JSON format) to the above-mentioned REST API Service Endpoint: (Rest Assured Post Example)

 

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 “dummy.restapiexample.com/api/v1” 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 request to an exact resource which is “/create” in this case. Hence, the complete Service Endpoint would be “http://dummy.restapiexample.com/api/v1/create” to which we are sending a POST request with the JSON request body. The JSON request body is nothing but the details about the new employee that we are looking to create. 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(“tammy”) : This line of code helps to check if the string ‘tammy’ present in the response or not.

d. assertEquals(200, response.getStatusCode()) : This line of code will throw true or false based on the condition if status code from the response matches with the value 200 or not.

 

3. How to validate the Response?

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

a. Check for the Response i.e {“name”:”tammy”,”salary”:”5000″,”age”:”20″,”id”:”78363″}

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

c. Check if the ‘tammy’ string is present in the response i.e ‘true’ as intended.

d. Lastly, we performed assertEquals verification for Status Code, which is successfully passed, hence no failure in the eclipse output.

Eclipse Console Output:

 

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

Simple representation of POST Request using RESTAssured...!!! Click To Tweet

 

If you like this post, please check out my other useful blog posts on Rest Assured:

Other Useful References:

 

Related Posts:

[wcp-carousel id=”5342″]
Series Navigation<< How to make a GET Request using Rest AssuredHow to send PUT Request in Rest Assured? (Updated 2020) >>

One thought on “How to send a POST Request using Rest Assured [2020]

Leave a Reply

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

Facebook Twitter Instagram YouTube