HTTP response interceptor to intercept the end of HttpRequest#execute() before returning
a successful response or throwing an exception for an unsuccessful response.
For example, this might be used to add a simple timer on requests:
public static class TimerResponseInterceptor implements HttpResponseInterceptor {
private final long startTime = System.nanoTime();
public void interceptResponse(HttpResponse response) {
long elapsedNanos = System.nanoTime() - startTime;
System.out.println("elapsed seconds: " + TimeUnit.NANOSECONDS.toSeconds(elapsedNanos) + "s");
}
}
Sample usage with a request factory:
public static HttpRequestFactory createRequestFactory(HttpTransport transport) {
return transport.createRequestFactory(new HttpRequestInitializer() {
Invoked at the end of HttpRequest#execute() before returning a successful response or
throwing an exception for an unsuccessful response.
Do not read from the content stream unless you intend to throw an exception. Otherwise, it
would prevent the caller of HttpRequest#execute() to be able to read the stream from
HttpResponse#getContent(). If you intend to throw an exception, you should parse the
response, or alternatively pass the response as part of the exception.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-08-28 UTC."],[],[],null,["# Interface HttpResponseInterceptor (2.0.0)\n\nVersion latestkeyboard_arrow_down\n\n- [2.0.0 (latest)](/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpResponseInterceptor)\n- [1.47.1](/java/docs/reference/google-http-client/1.47.1/com.google.api.client.http.HttpResponseInterceptor)\n- [1.46.3](/java/docs/reference/google-http-client/1.46.3/com.google.api.client.http.HttpResponseInterceptor)\n- [1.45.3](/java/docs/reference/google-http-client/1.45.3/com.google.api.client.http.HttpResponseInterceptor)\n- [1.44.2](/java/docs/reference/google-http-client/1.44.2/com.google.api.client.http.HttpResponseInterceptor)\n- [1.43.2](/java/docs/reference/google-http-client/1.43.2/com.google.api.client.http.HttpResponseInterceptor)\n- [1.42.3](/java/docs/reference/google-http-client/1.42.3/com.google.api.client.http.HttpResponseInterceptor)\n- [1.41.8](/java/docs/reference/google-http-client/1.41.8/com.google.api.client.http.HttpResponseInterceptor) \n\n public interface HttpResponseInterceptor\n\nHTTP response interceptor to intercept the end of [HttpRequest#execute()](/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpRequest#com_google_api_client_http_HttpRequest_execute__) before returning\na successful response or throwing an exception for an unsuccessful response.\n\nFor example, this might be used to add a simple timer on requests:\n\npublic static class TimerResponseInterceptor implements HttpResponseInterceptor {\n\nprivate final long startTime = System.nanoTime();\n\npublic void interceptResponse(HttpResponse response) {\nlong elapsedNanos = System.nanoTime() - startTime;\nSystem.out.println(\"elapsed seconds: \" + TimeUnit.NANOSECONDS.toSeconds(elapsedNanos) + \"s\");\n}\n}\n\nSample usage with a request factory:\n\npublic static HttpRequestFactory createRequestFactory(HttpTransport transport) {\nreturn transport.createRequestFactory(new HttpRequestInitializer() {\n\n@Override\npublic void initialize(HttpRequest request) {\nrequest.setResponseInterceptor(new TimerResponseInterceptor());\n}\n});\n}\n\nMore complex usage example:\n\npublic static HttpRequestFactory createRequestFactory2(HttpTransport transport) {\nfinal HttpResponseInterceptor responseInterceptor = new TimerResponseInterceptor();\nreturn transport.createRequestFactory(new HttpRequestInitializer() {\n\npublic void initialize(HttpRequest request) {\nrequest.setResponseInterceptor(new HttpResponseInterceptor() {\n\npublic void interceptResponse(HttpResponse response) throws IOException {\nresponseInterceptor.interceptResponse(response);\n}\n});\n}\n});\n}\n\nImplementations should normally be thread-safe.\n\nMethods\n-------\n\n### interceptResponse(HttpResponse response)\n\n public abstract void interceptResponse(HttpResponse response)\n\nInvoked at the end of [HttpRequest#execute()](/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpRequest#com_google_api_client_http_HttpRequest_execute__) before returning a successful response or\nthrowing an exception for an unsuccessful response.\n\nDo not read from the content stream unless you intend to throw an exception. Otherwise, it\nwould prevent the caller of [HttpRequest#execute()](/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpRequest#com_google_api_client_http_HttpRequest_execute__) to be able to read the stream from\n[HttpResponse#getContent()](/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpResponse#com_google_api_client_http_HttpResponse_getContent__). If you intend to throw an exception, you should parse the\nresponse, or alternatively pass the response as part of the exception."]]