Rest-assured how to give multiple query parameters in Java
Image in a situation where you want to search using multiple search parameters which are not unique , so we need to use Query parameters to filter and search the results .
Example- I want to search for a Users who are in page 2 , which has the id field value contains 9.So how to I achieve this ? In rest assured this is my URL
So first let’s break it down .
baseURI will be https://reqres.in/
basePath will be api/users
My query parameters will be
page and it’s value is 2 ,other query parameter is id and it’s value is 9(you can give either String or int)
So sample code is as per the below .
package com.basic.functions;import static io.restassured.RestAssured.given;import org.testng.annotations.Test;import io.restassured.RestAssured;import io.restassured.http.ContentType;public class performMultipleQueryparametrs {@Test(enabled=true)//Full URL https://reqres.in/api/users?page=2?,id=9public void performMultipleQueryparametr() {RestAssured.baseURI = "https://reqres.in/";RestAssured.basePath = "api/users";given().contentType(ContentType.JSON). with().queryParam("page", "2").queryParam("id", 9).when().get("").then().statusCode(200).log().all();}}
This will give below output as response.
{
“data”: {
“id”: 9,
“email”: “tobias.funke@reqres.in”,
“first_name”: “Tobias”,
“last_name”: “Funke”,
“avatar”: “https://s3.amazonaws.com/uifaces/faces/twitter/vivekprvr/128.jpg"
}
}