Rest Assured GET Request with Example

In this tutorial, we will learn Rest Assured GET request from basics.

We will also learn about POST, PUT, PATCH and DELETE request using Rest Assured.

In last tutorial we have learnt few terms- Endpoint, HTTP methods etc. which uses in REST. If you haven’t gone through that tutorial please read this What is REST?

In this post we will cover below point-

  • What is Rest Assured?
  • Uses
  • How to create a project
  • How to test API with Rest Assured GET request

Pre-requisites-

  • Java
  • IDE
  • Maven
  • TestNG

 

What is Rest Assured?

  • It is a Java library or API for testing RESTful web services.
  • It is used to test XML & JSON based web services.
  • It supports different HTTP methods – GET, POST, PUT, DELETE, OPTIONS, PATCH, HEAD.
  • It can be integrated with testing frameworks like JUnit, TestNG.
  • Rest Assured is implemented in Groovy.

 

Create First Project: Rest Assured GET request

Step 1: Open eclipse and create Maven project.

Step 2:  Add Rest Assured and TestNG dependencies in pom.xml file.

RestAssured project in eclipse

 

RestAssured Maven dependency

 

Step 3: Create a package under src/test/java folder.

Step 4: Create a class inside that package. See in below image.

Step 5:  Create a method with @Test annotation.

Step 6: Using RestAssured class and GET method we will get the response.

Response response = RestAssured.get("https://reqres.in/api/users?page=2");

Note: You can take URL from this site: https://reqres.in/. Above URL for GET method.

When you will hit url – https://reqres.in/api/users?page=2 , you will get below data.

Rest Assured GET request

 

Output-

There are so many methods in Response Interface which we can use for getting more details.

package Demo;

import org.testng.annotations.Test;

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

public class Test01_GET {
    
    
    @Test
    void test01() {
        
        
    Response response = RestAssured.get("https://reqres.in/api/users?page=2");
    System.out.println(response.getBody().asString());
    System.out.println(response.asString());
    System.out.println(response.getStatusCode());
    System.out.println(response.getHeader("content-type"));
    System.out.println(response.getTime());
        
        
    }
}

Output-

 

Step 7:  After getting response we have to validate this response using assertion.

Here I will validate status code.

package Demo;

import org.testng.Assert;
import org.testng.annotations.Test;

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

public class Test01_GET {
    
    
    @Test
    void test01() {
        
        
    Response response = RestAssured.get("https://reqres.in/api/users?page=2");
    System.out.println(response.getBody().asString());
    System.out.println(response.asString());
    System.out.println(response.getStatusCode());
    System.out.println(response.getHeader("content-type"));
    System.out.println(response.getTime());
    
    // Validate status code
    int statusCode = response.getStatusCode();
    Assert.assertEquals(statusCode, 200);
        
        
    }
}


BDD Style

Above code we can write in BDD style means using given(), when() and then() keywords.

In given section we can use GET method and then section we can validate our data.

package Demo;

import io.restassured.RestAssured;
import io.restassured.path.json.JsonPath;
import io.restassured.response.Response;

import static io.restassured.RestAssured.*;
import static org.hamcrest.Matchers.*;

import org.testng.Assert;
import org.testng.annotations.Test;

public class Test01_GET {

    @Test
    void test01() {

        Response response = RestAssured.get("https://reqres.in/api/users?page=2");
        System.out.println(response.getBody().asString());
        System.out.println(response.asString());
        System.out.println(response.getStatusCode());
        System.out.println(response.getHeader("content-type"));
        System.out.println(response.getTime());

        // Validate status code
        int statusCode = response.getStatusCode();
        Assert.assertEquals(statusCode, 200);

    }

    @Test
    void test02() {

        given()
        .get("https://reqres.in/api/users?page=2")
          .then()
            .statusCode(200).body("data.id[0]", equalTo(7));

    }
}

GET request in BDD Style

 

Note: In above code I am verifying status code and id from response. In console response we can see there is multiple ids under data object. I am verifying first id that is why it is data.id[0] as first object considers as 0 index.


Rest assured POST request example

HTTP POST request is used to post/create the data on the server.

We will use JSONObject class to put the values which we need to paas into POST request.

Before using JSONObject, we have to add maven dependency in pom.xml file.

        <dependency>
            <groupId>com.googlecode.json-simple</groupId>
            <artifactId>json-simple</artifactId>
            <version>1.1.1</version>
        </dependency>

Rest Assured POST JSONObject request example

package Demo;

import org.json.simple.JSONObject;
import org.testng.annotations.Test;

import io.restassured.http.ContentType;
import io.restassured.response.Response;
import static io.restassured.RestAssured.*;

public class Test01_POST {
    
    @Test
    public void test1_post() {
        
        JSONObject request = new JSONObject();
        
        request.put("name", "Ajeet");
        request.put("Job", "Teacher");
        
        
        given().
        header("Content-type","application/json").
        contentType(ContentType.JSON).
        body(request.toJSONString()).
        when().post("https://reqres.in/api/users").
        then().statusCode(201).log().all();
        
        
        
        
    }
    

}

Output-

Rest Assured POST Request


Rest Assured PUT request example

HTTP PUT request is used to update the resources on the server. In this request we need to send full payload.

package Demo;

import static io.restassured.RestAssured.given;

import org.json.simple.JSONObject;
import org.testng.annotations.Test;

import io.restassured.http.ContentType;

public class Test01_PUT {
    
    
    
    @Test
    public void test1_post() {
        
        JSONObject request = new JSONObject();
        
        request.put("name", "Aman");
        request.put("Job", "Teacher");
        
        
        given().
        header("Content-type","application/json").
        contentType(ContentType.JSON).
        body(request.toJSONString()).
        when().
           put("https://reqres.in/api/users/2").
        then().
           statusCode(200).log().all();
        
    }

}

Output:

Rest Assured PUT Request

 


Rest Assured PATCH request example

HTTP PATCH request used to update the resources on the server but in this case we need to send only that field which we want to update.

In below code, I want to update only job title.

package Demo;

import static io.restassured.RestAssured.given;

import org.json.simple.JSONObject;
import org.testng.annotations.Test;

import io.restassured.http.ContentType;

public class Test01_PATCH {
    

    @Test
    public void test1_post() {
        
        JSONObject request = new JSONObject();
        
        //request.put("name", "Aman");
        request.put("Job", "Dev");
        
        
        given().
        header("Content-type","application/json").
        contentType(ContentType.JSON).
        body(request.toJSONString()).
        when().
           patch("https://reqres.in/api/users/2").
        then().
           statusCode(200).log().all();
            
    }

}

Output:

Rest Assured PATCH request

 


Rest Assured DELETE request example

DELETE request is used to delete a resource from the server.

package Demo;

import static io.restassured.RestAssured.given;

import org.json.simple.JSONObject;
import org.testng.annotations.Test;

import io.restassured.http.ContentType;

public class Test01_DELETE {
    
    

    @Test
    public void test1_post() {
                
        
        given().
           delete("https://reqres.in/api/users/2").
        then().
           statusCode(204).log().all();
        
        }

}

Output:

Rest Assured Delete Request


Watch this Video- 

 

This is all about GET, POST, PUT, PATCH and DELETE request using RestAssured. I hope this post will be helpful for you.

Summary:

In this post, we have coveredRest Assured GET Requestusing java coding in Eclipse.

 

Leave a Comment