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”. post request rest assured  post request rest assured.  post request rest assured.  post request rest assured. post request rest assured. post request rest assured rest assured post example rest assured post request rest assured post example

Simple representation of POST JSON Request using REST Assured...!!! Share on X

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 ‘Reqres.in Sample Rest API’ which is available here. This page contains Fake Online REST API for the testing purpose which is performing various CRUD operations.

Let’s take an example of one of the API POST endpoint available at the above-mentioned website which is ‘/users’. The full-service URL with the endpoint is http://reqres.in/api/users

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

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 “reqres.in/api” 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 “/users” in this case. Hence, the complete Service Endpoint would be “http://reqres.in/api/users” 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 user 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(“Chris”) : This line of code helps to check if the string ‘Chris’ 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”:”Chris”,”job”:”Java Developer”,”id”:”152″,”createdAt”:”2022-04-21T08:15:11.880Z”}

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

c. Check if the ‘Chris’ 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...!!! Share on X

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

Other Useful References:

rest assured post request
post request rest assured

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