user2451016
user2451016

Reputation: 1961

How to handle multiple path parameters in RestAssured?

We have a restAssured GET call:

 public Response getRequest( final int survey_ID){
    httpRequest.header("Content-Type", ContentType.JSON);
    httpRequest.header("Authorization", System.getProperty("apiKey"));
    httpRequest.pathParams("survey_id", survey_ID);
    return httpRequest.get("/{survey_id}");
}

Now there's new end-point which accepts multiple path parms like:

 goals/check-enabled?survey_ids=54321,12345

[Here Survey_ids could be 1 or many]

So, how could I handle this in my code? I was thinking to implement something like below, but I suppose there could be a better way.

  public Response getRequest( final int[] survey_IDs){
    httpRequest.header("Content-Type", ContentType.JSON);
    httpRequest.header("Authorization", 
    System.getProperty("apiKey"));
//        another Option
//        for(int survey_ID : survey_IDs){
//            httpRequest.pathParams("survey_id", survey_ID);
//        }
        return httpRequest.get("/{survey_ids}", survey_IDs);
    }

Please suggest

Thanks a lot

Upvotes: 1

Views: 2213

Answers (2)

blafoo
blafoo

Reputation: 126

Right now you are having exactly ONE query parameter (because of the ?param=value syntax) even if it looks like a path parameter with a list.

You can choose between these options:

  1. Specify a multi-value query parameter that'll be sent with the request e.g:
...given().queryParam("survey_ids", asList("54321", "12345"))...;

This will set the parameter survey_ids=54321 and survey_ids=12345

The method needs to be adjusted this way:

public Response getRequest(List<Integer> survey_IDs) {
  1. Stay using one query parameter but split it.
String[] ids = survey_IDs.split(",");
...given().queryParam("survey_ids", "54321,12345")...;

Upvotes: 1

Chris
Chris

Reputation: 34055

The URL example you give in the question, i.e., goals/check-enabled?survey_ids=54321,12345 shows survey_ids being a query parameter, not path parameter as you describe. A path parameter would be something like this: /items/{item_id}, where you need to pass the item id, for example, /items/2. Maybe you could try something like the below, as described here, for passing multiple values for a query parameter (which seems to be what you are looking for).

String survey_IDs= "54321,12345";
RestAssured.given().urlEncodingEnabled(false).
    contentType(ContentType.JSON).with().
    queryParam("survey_ids", survey_IDs).
    when().get("").then().statusCode(200).log().all();

Upvotes: 1

Related Questions