Sunday, 24 November 2019

Rest Assured: Set Request query parameters

‘param’ method is used to set query parameters.

Example 1
Response response = given().param("firstName", "Gopi").param("lastName", "Battu")
.header("Accept", "application/json").when().get("api/v1/employees/by-names").thenReturn();

Example 2
Map<String, Object> parametersMap = new HashMap<>();
parametersMap.put("firstName", "Gopi");
parametersMap.put("lastName", "Battu");
response = given().params(parametersMap).header("Accept", "application/json").when()
.get("api/v1/employees/by-names").thenReturn();

App.java
package com.sample.app;

import static io.restassured.RestAssured.given;

import java.util.HashMap;
import java.util.Map;

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

public class App {
    private static int requestCount = 0;

    private static void printResponse(Response response) {
        String respStr = response.getBody().asString();
        System.out.println("For " + (++requestCount) + " request");
        System.out.println(respStr + "\n");
    }

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

        Response response = given().param("firstName", "Gopi").param("lastName", "Battu")
                .header("Accept", "application/json").when().get("api/v1/employees/by-names").thenReturn();
        printResponse(response);

        Map<String, Object> parametersMap = new HashMap<>();
        parametersMap.put("firstName", "Gopi");
        parametersMap.put("lastName", "Battu");
        response = given().params(parametersMap).header("Accept", "application/json").when()
                .get("api/v1/employees/by-names").thenReturn();
        printResponse(response);
    }
}

Output
For 1 request
[{"id":7,"firstName":"Gopi","lastName":"Battu"}]

For 2 request
[{"id":7,"firstName":"Gopi","lastName":"Battu"}]




Previous                                                    Next                                                    Home

No comments:

Post a Comment