MeetAhsan Logo

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.

Courses

Extensive range of courses to choose from.

Testimonials

MeetAhsan successful attainment of a goal or the accomplishment of a significant task or objective.

Meet Ahsan Training Institute is one of the best IT Training Institutes in Pakistan. It has been providing quality IT education since 2016. 100+ batches of professional IT courses have been completed at Meet Ahsan Institute in Lahore.

© 2024 Meet Ahsan | All Right Reserved