Show List

Testing GET Request

Below is a simple Java program using Rest Assured to test a GET request to a REST API endpoint. This example assumes that you have Rest Assured and its dependencies properly set up in your Java project.


import io.restassured.RestAssured; import io.restassured.response.Response; public class ApiTest { public static void main(String[] args) { // Set base URI of the REST API RestAssured.baseURI = "https://jsonplaceholder.typicode.com"; // Send GET request to /posts/1 endpoint Response response = RestAssured.get("/posts/1"); // Get and print the response body String responseBody = response.getBody().asString(); System.out.println("Response Body:"); System.out.println(responseBody); // Get and print the status code int statusCode = response.getStatusCode(); System.out.println("\nStatus Code: " + statusCode); // Get and print the content type String contentType = response.getContentType(); System.out.println("Content Type: " + contentType); } }

In this example:

  • We set the base URI of the REST API using RestAssured.baseURI.
  • We send a GET request to the /posts/1 endpoint using RestAssured.get("/posts/1").
  • We retrieve the response body, status code, and content type from the response object and print them out.

You can modify the base URI and endpoint according to the API you want to test. Make sure you have Rest Assured and its dependencies added to your project's classpath. Additionally, you may need to handle exceptions such as IOException or NullPointerException depending on your specific requirements and error handling strategy.


    Leave a Comment


  • captcha text