GET request in Rest Assured

 GET Request in Http request is used for fetching data from server. Suppose we want to know information of weather of the particular city then you send GET request to server in form of HTTP protocol and server will return weather information to you in JSON or XML format.

How to write GET request in Rest Assured                    

  1. import static io.restassured.RestAssured.*;
    import io.restassured.RestAssured;
    public class get {
    public static void main(String[] args) {
    RestAssured.baseURI= "https://reqres.in";
    String response=given().queryParam("page","2").when().
    get("/api/users").then().assertThat().statusCode(200).extract().response().asString();
    System.out.println(response);
    }}  
Code Explanation
Rest Assured worked on three key words i.e. given(), when()and then().
First we need to set baseURI to send request to server using RestAssured class.
In given() we specify preconditions we have like query parameters, headers and body in case of POST and PUT requests.
In then() we specify type of request we wish to send to server,
in our case GET request and pass path parameter as argument in method type.
In then() we validate the response like status code and response body.

0 comments:

Post a Comment