Skip to content
Advertisement

How to intercept a request in Apache AsyncHttpClient?

Is there a way to intercept a request made by AsyncHttpClient and do some operations based on the request data.

I want to do something like this:

    interceptor = new Interceptor(){
        process(request){
           //do something  
        }
    }
    
    client.addInterceptor(interceptor);

is something like this possible?

Advertisement

Answer

I did it using ‘filters’.

AsyncHttpClientConfig.Builder configBuilder = new AsyncHttpClientConfig.Builder();
configBuilder.addResponseFilter(new ResponseFilter() {
    public FilterContext filter(FilterContext ctx) throws FilterException {
              //do something
        }
    }
});
AsyncHttpClient c = new AsyncHttpClient(configBuilder.build());

Similarly you can add a request filter

configbuilder.addRequestFilter(RequestFilterImpl);
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement