1

Is there a way to construct Url with query parameters where the value of the query param is not string. For String values of query parameter there are APIs in RestAssured or apache httpClient but I could not find any such API matching my needs.

e.g. Query param is:

        Map queryParam = ImmutableMap.of("activityTypeList", Arrays.asList(1,2,3));

For String valued query parameters, I am following the below approach:

    public String urlBuilder(String url, Map<String, String> queryParam) throws URISyntaxException{
        URIBuilder uriBuilder = new URIBuilder(url);
        for(Map.Entry<String, String> entry: queryParam.entrySet()){
            uriBuilder.addParameter(entry.getKey(), entry.getValue());
        }
        return uriBuilder.toString();
    }

I understand this can be achieved by:

request.queryParam("activityTypeList", Arrays.asList(1,2,3));

where request is an object of type RequestSpecification. I am more interested in the URL construction approach.

A followup question: Is there an API which provides the URL that the RestAssured client hits after URL construction; would be much helpful for logging.

Any lead in RestAssured/httpClient/okHttp would really help.

1

I am not sure what you are trying to do, your code fails because

 public String urlBuilder(String url, Map<String, String> queryParam) 

In the parameter definition, you type casts it as a Map of Map<String, String> and then you are passing a map of Map<String, ArrayList>

So just changing that will fix the issue:

public String urlBuilder(String url, Map<String, Object> queryParam) throws URISyntaxException{
        URIBuilder uriBuilder = new URIBuilder(url);
        for(Map.Entry<String, Object> entry: queryParam.entrySet()){

            System.out.println(entry.getValue());
            uriBuilder.addParameter(entry.getKey(), entry.getValue().toString());
        }
        return uriBuilder.toString();
    }

More cleaner code:

ImmutableMap<String, List<Integer>> queryParam = ImmutableMap.of("activityTypeList", Arrays.asList(1,2,3));

and

public String urlBuilder(String url, Map<String, List<Integer>> queryParam) throws URISyntaxException{
        URIBuilder uriBuilder = new URIBuilder(url);
        for(Entry<String, List<Integer>> entry: queryParam.entrySet()){

            System.out.println(entry.getValue());
            uriBuilder.addParameter(entry.getKey(), entry.getValue().toString());
        }
        return uriBuilder.toString();
    }

You can achieve logging by using:

 RestAssured.given().
 filter(new RequestLoggingFilter()).
 filter(new ResponseLoggingFilter()).
 when().get("/user");

If you want to redirect it to a print stream then:

     StringWriter requestwrite = new StringWriter();
 PrintStream  requestcapture = new PrintStream(new WriterOutputStream(requestwrite),true);

 StringWriter reswrite = new StringWriter();
 PrintStream  rescapture = new PrintStream(new WriterOutputStream(reswrite),true);


 RestAssured.given().
 filter(new RequestLoggingFilter(requestcapture)).
 filter(new ResponseLoggingFilter(rescapture)).
 when().get("/user");


 System.err.println(requestwrite.toString());
 System.err.println(reswrite.toString());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.