restrest-assuredweb-api-testing

Can we extract the full URL with query parameters in RestAssured


I'm working on a RestAssured project and for some debugging purposes waant to print the full url to which the request is sent.

In the project that I'm working on we set the ApiHost, BasePath, uri and query parameters separately in test methods, and as of now we concatenate these together and print the full path.

But I want to know if there's a way to print the full path/url (including query parameters) to which the request is sent, without adding the parts as we do.

I have done some research and found 'QueryableRequestSpecification' can be used to extract request details. I have used 'queryable.getURI()' and tried some other available methods. But queryable.getURI() extract the uri without the query params.

Is there any other way ?


Solution

  • You can achieve that with help of request filters:

    public static void main(String[] args) throws IOException {
    
        RestAssured
                .given()
                .baseUri("https://httpbin.org")
                .basePath("/get/{deviceId}")
                .queryParam("param", "value")
                .pathParam("deviceId", 123)
                .filter(new Filter() {
                    @Override
                    public Response filter(
                            FilterableRequestSpecification requestSpec, 
                            FilterableResponseSpecification responseSpec, 
                            FilterContext ctx) {
                                System.out.println(
                                        requestSpec.getURI()
                                );
                                return ctx.next(requestSpec, responseSpec);
                    }
                })
                .get();
    
    }
    

    Output:

    https://httpbin.org/get/123?param=value