Skip to content

Meet Ahsan

How to Verify API Response Using RestAssured

Here is an example of how you can use RestAssured to verify the API response in Java:

import io.restassured.RestAssured;
import io.restassured.response.Response;
import io.restassured.specification.RequestSpecification;

public class Main {
  public static void main(String[] args) {
    // Set the base URI
    RestAssured.baseURI = "https://api.example.com";

    // Create a request specification
    RequestSpecification request = RestAssured.given();

    // Set the API endpoint and any necessary query parameters
    request.queryParam("param1", "value1");
    request.queryParam("param2", "value2");

    // Send a GET request to the API
    Response response = request.get("/endpoint");

    // Verify the API response
    response.then().statusCode(200);
    response.then().body("key1", equalTo("value1"));
    response.then().body("key2", equalTo("value2"));
  }
}

In this example, the then method is used to verify the status code and the values of the keys “key1” and “key2” in the body of the response. You can use other methods such as contentType, header, and time to verify other aspects of the response.

I hope this example helps you understand how to verify the API response using RestAssured in Java.

Admission Form