Kong / unirest-java

Unirest in Java: Simplified, lightweight HTTP client library.
http://kong.github.io/unirest-java/
MIT License
2.6k stars 592 forks source link

Add Support for HttpResponseInterceptor #307

Closed jogura closed 4 years ago

jogura commented 4 years ago

Is your feature request related to a problem? Please describe. I would like the ability to add a HttpResponseInterceptor to preprocess the response. I have a use-case where I'm configuring a "base" URL with standard headers and returning an UnirestInstance to the client. I would like to use the HttpResponseInterceptor to put common logic in all responses. So I may not have access to the ifSuccess() method.

Describe the solution you'd like The current implementation already supports the ability to add multiple HttpRequestInterceptors. The solution would model the same pattern but allow for HttpResponseInterceptors to the Config instance.

Alternative You could "intercept" using the ifSuccess() method. However, in my particular use-case I don't have access to when the UnirestInstance is invoked. So I'd like to configure the response interceptor in the Config object.

ryber commented 4 years ago

As it stands the HttpRequestInterceptor exposed is one of the last pieces of Unirest's leaky apache abstraction. So I'd rather not make that leak bigger. Would you be OK if this interceptor was part of Unirest and not the apache class?

Also, maybe you could give me some ideas of what you would want to do with it? Would you see this as modifying the body in some way? Something created by the object mapper? Consuming and logging the responses? A few examples would be great

ryber commented 4 years ago

any input @jogura ? I'm trying to understand what exactly such a interceptor would do?

ryber commented 4 years ago

3.2.00 now supports a Unirest specific interceptor that has methods for onRequest and onResponse (and a error handler for connection failures)