How to pass parameters to Rest-Assured

Uday picture Uday · Sep 9, 2015 · Viewed 55.6k times · Source

Can someone help me in this scenario:

When I invoke this service, http://restcountries.eu/rest/v1/, I get couple of countries information.

But when I want to get any specific country information like say Finland, I invoke the web service as http://restcountries.eu/rest/v1/name/Finland to get the country related info.

To automate the above scenario, how can I parameterize the country name in Rest-Assured? I tried below, but doesn't help me.

RestAssured.given().
                    parameters("name","Finland").
            when().
                    get("http://restcountries.eu/rest/v1/").
            then().
                body("capital", containsString("Helsinki"));

Answer

рüффп picture рüффп · Oct 5, 2015

As the documentation explains:

REST Assured will automatically try to determine which parameter type (i.e. query or form parameter) based on the HTTP method. In case of GET query parameters will automatically be used and in case of POST form parameters will be used.

But in your case it seems you need path parameter instead query parameters. Note as well that the generic URL for getting a country is http://restcountries.eu/rest/v1/name/{country} where {country} is the country name.

Then, there are as well multiple ways to transfers path parameters.

Here are few examples

Example using pathParam():

// Here the key name 'country' must match the url parameter {country}
RestAssured.given()
        .pathParam("country", "Finland")
        .when()
            .get("http://restcountries.eu/rest/v1/name/{country}")
        .then()
            .body("capital", containsString("Helsinki"));

Example using variable:

String cty = "Finland";

// Here the name of the variable have no relation with the URL parameter {country}
RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/name/{country}", cty)
        .then()
            .body("capital", containsString("Helsinki"));

Now if you need to call different services, you can also parametrize the "service" like this:

// Search by name
String val = "Finland";
String svc = "name";

RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Helsinki"));


// Search by ISO code (alpha)
val = "CH"
svc = "alpha"

RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Bern"));

// Search by phone intl code (callingcode)
val = "359"
svc = "callingcode"

RestAssured.given()
        .when()
            .get("http://restcountries.eu/rest/v1/{service}/{value}", svc, val)
        .then()
            .body("capital", containsString("Sofia"));

After you can also easily use the JUnit @RunWith(Parameterized.class) to feed the parameters 'svc' and 'value' for a unit test.