You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@hc.apache.org by "Oleg Kalnichevski (Jira)" <ji...@apache.org> on 2022/10/23 10:38:00 UTC

[jira] [Commented] (HTTPCLIENT-2241) HttpResponseInterceptor is never called on errors

    [ https://issues.apache.org/jira/browse/HTTPCLIENT-2241?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17622778#comment-17622778 ] 

Oleg Kalnichevski commented on HTTPCLIENT-2241:
-----------------------------------------------

[~patrickjamesbarry] I am not sure I understand the problem. This works for me.
{code:java}
CloseableHttpClient httpclient = HttpClients.custom()
        .addInterceptorFirst(new HttpResponseInterceptor() {
            @Override
            public void process(HttpResponse httpResponse, HttpContext httpContext) throws HttpException, IOException {
                System.out.println("Got response: " + httpResponse);
            }
        })
        .build();
try {
    HttpGet httpget = new HttpGet("http://httpbin.org/huh");
    System.out.println("Executing request " + httpget.getRequestLine());

    // Pass local context as a parameter
    CloseableHttpResponse response = httpclient.execute(httpget);
    try {
        System.out.println("----------------------------------------");
        System.out.println(response.getStatusLine());
        EntityUtils.consume(response.getEntity());
    } finally {
        response.close();
    }
} finally {
    httpclient.close();
}
{code}
{noformat}
Executing request GET http://httpbin.org/huh HTTP/1.1
Got response: HttpResponseProxy{HTTP/1.1 404 NOT FOUND [Date: Sun, 23 Oct 2022 10:35:41 GMT, Content-Type: text/html, Content-Length: 233, Connection: keep-alive, Server: gunicorn/19.9.0, Access-Control-Allow-Origin: *, Access-Control-Allow-Credentials: true] ResponseEntityProxy{[Content-Type: text/html,Content-Length: 233,Chunked: false]}}
----------------------------------------
HTTP/1.1 404 NOT FOUND
{noformat}
Oleg

> HttpResponseInterceptor is never called on errors
> -------------------------------------------------
>
>                 Key: HTTPCLIENT-2241
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-2241
>             Project: HttpComponents HttpClient
>          Issue Type: Bug
>          Components: HttpClient (classic)
>    Affects Versions: 4.5.13
>            Reporter: Patrick Barry
>            Priority: Major
>
> We are registering two interceptors for logging purposes. One for request and response. Our response Interceptor is never called when we encounter an error, like a 404.   Is there another interceptor or way to capture this info or is this a bug. We are using Jersey with Apache client under the covers.  
> Here is a unit test that that requires Slf4j, logback and apache http client.
> {code:java}
> @Test
> public void testResponseLoggingInterceptor2(){
> Logger logger = (Logger) LoggerFactory.getLogger(ResponseLoggingInterceptor.class);
> // create and start a ListAppender
> ListAppender<ILoggingEvent> listAppender = new ListAppender<>();
> listAppender.start();
> // add the appender to the logger
> logger.addAppender(listAppender);
> ClientConfig clientConfig = new ClientConfig();
> clientConfig.connectorProvider(new ApacheConnectorProvider());
> Client client = ClientBuilder.newBuilder().withConfig(clientConfig).build();
> client.register((ApacheHttpClientBuilderConfigurator) httpClientBuilder ->
> httpClientBuilder
> .addInterceptorLast(new RequestLoggingInterceptor())
> .addInterceptorFirst(new ResponseLoggingInterceptor()));//This is never hit when hitting endpoint that does not exists
> try {
> WebTarget target = client.target("https://localhost:8080/not/exists");
> Response response = target.request("application/json").get();
> }catch (Exception ignored){
> //nothing
> }
> List<ILoggingEvent> logsList = listAppender.list;
> assertEquals("test message", logsList.get(0).getFormattedMessage());
> }{code}
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@hc.apache.org
For additional commands, e-mail: dev-help@hc.apache.org