REST Assured HTTP Post Request

In this REST Assured tutorial, you will learn how to make HTTP Post request and validate the Response. To break it into more details you will learn:

  • Make HTTP Post request 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

To be able 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 JSON object which we will set as a body of our HTTP Post request.

Adding Google Gson to your project

One of the ways to easily create JSON objects is to use Google Gson library. Open 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>

Or if you do not use maven and manage Java libraries yourself then you can download Gson library jar file from this link.

Once you 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 have created earlier is then set to Http Request body as a String.

Step 3: Send HTTP Post Request

And finally, we can send 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 HTTP Post request containing JSON payload with user login credentials and then validates 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);
    }
}

I hope this REST Assured tutorial was helpful for you. To learn more about how to test RESTful Web Services with REST Assured please check other tutorials I have published in the REST Assured category and if you enjoy learning by following step-by-step video tutorials then check the list of video courses below. One of them might help you to greatly speed up your learning progress.