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:
- What is HTTP POST Request?
- How to send a POST request using Rest Assured?
- How to validate the Response?
Let’s begin:
1. What is HTTP POST Request?
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:
1 2 3 4 | { "name": "Chris", "job": "Java Developer" } |
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)
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 37 38 39 40 41 42 43 44 | /** * POST Request * @author Deepak Verma */ import static org.testng.Assert.assertEquals; import org.testng.annotations.Test; import io.restassured.RestAssured; import io.restassured.http.ContentType; import io.restassured.response.Response; public class RestAssured_PostRequest { @Test public void createUser() { RestAssured.baseURI = "https://reqres.in/api"; String requestBody = "{\n" + " \"name\": \"Chris\",\n" + " \"job\": \"Java Developer\"\n" + "}"; Response response = null; try { response = RestAssured.given() .contentType(ContentType.JSON) .body(requestBody) .post("/users"); } catch (Exception e) { e.printStackTrace(); } System.out.println("Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); System.out.println("Does Reponse contains 'Chris'? :" + response.asString().contains("Chris")); assertEquals(201, response.getStatusCode()); } } |
Let’s try to understand the code:
1. Setting up Base URI
1 | RestAssured.baseURI = "http://reqres.in/api"; |
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
1 2 3 4 5 | Response response = null; response = RestAssured.given() .contentType(ContentType.JSON) .body(requestBody) .post("/users"); |
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
1 2 3 4 5 | System.out.println("Response :" + response.asString()); System.out.println("Status Code :" + response.getStatusCode()); System.out.println("Does Reponse contains 'Chris'? :" + response.asString().contains("Chris")); assertEquals(201, response.getStatusCode()); |
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:
1 2 3 4 5 6 7 8 9 10 | [RemoteTestNG] detected TestNG version 7.0.1 Response :{"name":"Chris","job":"Java Developer","id":"114","createdAt":"2022-04-21T08:16:11.735Z"} Status Code :201 Does Reponse contains 'Chris'? :true PASSED: createUser =============================================== Default test Tests run: 1, Failures: 0, Skips: 0 =============================================== |
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 XIf you like this post, please check out my other useful blog posts on Rest Assured:
Other Useful References:
nice work..!!