Sunday, 24 November 2019

Rest Assured: GET Request Example

Step 1: Set base uri.
RestAssured.baseURI = "http://localhost:8080";

Step 2: Use given() method to send the request with given input like parameters, headers etc.,

Response response = given().header("Accept", "application/json").when().get("api/v1/employees").thenReturn();

Above snippet hit the request http://localhost:8080/api/v1/employees by setting the header 'Accept' to 'application/json'.

App.java
package com.sample.app;

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

import static io.restassured.RestAssured.given;

public class App {
    public static void main(String args[]) {
        RestAssured.baseURI = "http://localhost:8080";

        Response response = given().header("Accept", "application/json").when().get("api/v1/employees").thenReturn();

        String respStr = response.getBody().asString();
        System.out.println(respStr);
    }
}

Output
[{"id":1,"firstName":"Deepak","lastName":"Moud"},{"id":2,"firstName":"Srinivasa Rao","lastName":"Gumma"},{"id":3,"firstName":"Purna Chandra","lastName":"Rao"},{"id":4,"firstName":"Madhavi Latha","lastName":"Gumma"},{"id":5,"firstName":"Raghava","lastName":"Reddy"},{"id":6,"firstName":"Ram","lastName":"Gurram"},{"id":7,"firstName":"Gopi","lastName":"Battu"},{"id":8,"firstName":"Gopi","lastName":"Gumma"},{"id":9,"firstName":"Radha","lastName":"Krishna"}]



Previous                                                    Next                                                    Home

No comments:

Post a Comment