This REST Assured tutorial will teach you how to make an HTTP POST request and validate the Response. To break it into more detail, you will learn:
- Make HTTP Post requests with REST Assured,
- Create JSON Object using the com.google.gson library,
- Send JSON payload in the body of HTTP Post request,
- Validate the Response
Read the following tutorial to validate the HTTP response status code.
To test your RESTful Web Service endpoints with REST Assured and send HTTP Post or GET requests, you will need to add REST Assured to your project. Click on the following link to read the tutorial on how to add Rest Assured to your API project.
Step 1: HTTP Post Request Body
RESTful Web Services usually consume information sent to them via HTTP Post request as JSON or XML Payload. The payload is sent via HTTP Request Body. Let’s first learn how to create a JSON object that we will set as a body of an HTTP POST request.
Adding Google Gson to your project
One of the ways to easily create JSON objects is to use the Google Gson library. Open the pom.xml file of your Java project and add the below dependency.
<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson --> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency>
Once you have added the Google Gson library to your project, you can create a new JSON object the following way:
// Create new JSON Object JsonObject loginCredentials = new JsonObject(); loginCredentials.addProperty("email", "[email protected]"); loginCredentials.addProperty("password", "123");
Step 2: Create REST Assured Request
To send HTTP Post request to a RESTful Web Service endpoint with REST Assured, use the following code snippet:
RestAssured.baseURI = "http://localhost:8080"; RequestSpecification httpRequest = RestAssured.given(); httpRequest.header("Content-Type", "application/json"); httpRequest.body(loginCredentials.toString());
Please note the Content-Type request Header, which has a value of “application/json”. This is because the content of our HTTP POST Request body will be a JSON message. Please also note that the JsonObject we created earlier is set to the HTTP Request body as a String.
Step 3: Send HTTP Post Request
And finally, we can send an HTTP Post request to a RESTful Web Service endpoint.
Response response = httpRequest.post("/login");
HTTP Post Request Complete Code Example
Below is a complete Test class source which demonstrates how to use REST Assured to test a RESTful Web Service endpoint by sending it an HTTP POST request containing JSON payload with user login credentials and then validating the Response object Headers to make sure that the required headers are present.
import com.google.gson.JsonObject; import io.restassured.RestAssured; import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; import org.junit.Assert; import org.junit.Test; public class APITest { @Test public void validate_response_headers_test() { RestAssured.baseURI = "http://localhost:8888"; RequestSpecification httpRequest = RestAssured.given(); httpRequest.header("Content-Type", "application/json"); // Create new JSON Object JsonObject loginCredentials = new JsonObject(); loginCredentials.addProperty("email", "[email protected]"); loginCredentials.addProperty("password", "123"); httpRequest.body(loginCredentials.toString()); Response response = httpRequest.post("/login"); String authorizationHeader = response.getHeader("Authorization"); Assert.assertNotNull(authorizationHeader); } }
Video Tutorial
If you’re seeking to enhance your skills in testing RESTful web services, make sure to check out our tutorial on the Testing Java Code page. Discover how to utilize REST Assured to perform comprehensive API testing and ensure the quality and reliability of your services.
If you enjoy learning by following step-by-step video tutorials, check the list of video courses below. One of them might help you greatly speed up your learning progress.