# Interface HttpUnsuccessfulResponseHandler (2.1.0)

<button class="button button-with-icon cloud-select-dropdown__button " aria-haspopup="true">Version latest</button>

- [2.1.0 (latest)](https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [2.0.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/2.0.3/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [1.47.1](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.47.1/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [1.46.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.46.3/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [1.45.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.45.3/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [1.44.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.44.2/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [1.43.2](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.43.2/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [1.42.3](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.42.3/com.google.api.client.http.HttpUnsuccessfulResponseHandler)
- [1.41.8](https://docs.cloud.google.com/java/docs/reference/google-http-client/1.41.8/com.google.api.client.http.HttpUnsuccessfulResponseHandler)

    public interface HttpUnsuccessfulResponseHandler

Interface which handles abnormal HTTP responses (in other words not 2XX).

For example, this might be used to refresh an OAuth 2 token:

public static class RefreshTokenHandler implements HttpUnsuccessfulResponseHandler {
public boolean handleResponse(
HttpRequest request, HttpResponse response, boolean retrySupported) throws IOException {
if (response.getStatusCode() == HttpStatusCodes.STATUS_CODE_UNAUTHORIZED) {
refreshToken();
}
return false;
}
}

Sample usage with a request factory:

public static HttpRequestFactory createRequestFactory(HttpTransport transport) {
final RefreshTokenHandler handler = new RefreshTokenHandler();
return transport.createRequestFactory(new HttpRequestInitializer() {
public void initialize(HttpRequest request) {
request.setUnsuccessfulResponseHandler(handler);
}
});
}

More complex usage example:

public static HttpRequestFactory createRequestFactory2(HttpTransport transport) {
final RefreshTokenHandler handler = new RefreshTokenHandler();
return transport.createRequestFactory(new HttpRequestInitializer() {
public void initialize(HttpRequest request) {
request.setUnsuccessfulResponseHandler(new HttpUnsuccessfulResponseHandler() {
public boolean handleResponse(
HttpRequest request, HttpResponse response, boolean retrySupported)
throws IOException {
return handler.handleResponse(request, response, retrySupported);
}
});
}
});
}

## Methods

### handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)

    public abstract boolean handleResponse(HttpRequest request, HttpResponse response, boolean supportsRetry)

Handler that will be invoked when an abnormal response is received. There are a few simple
rules that one must follow:

- If you modify the request object or modify its execute interceptors in a way that should resolve the error, you must return true to issue a retry.
- Do not read from the content stream, this will prevent the eventual end user from having access to it.

| **Parameters** ||
|---|---|
| **Name** | **Description** |
| `request` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpRequest` Request object that can be read from for context or modified before retry |
| `response` | `https://docs.cloud.google.com/java/docs/reference/google-http-client/latest/com.google.api.client.http.HttpResponse` Response to process |
| `supportsRetry` | `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` Whether there will actually be a retry if this handler return ` true`. Some handlers may want to have an effect only when there will actually be a retry after they handle their event (e.g. a handler that implements exponential backoff). |

| **Returns** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html` | Whether or not this handler has made a change that will require the request to be re-sent. |

| **Exceptions** ||
|---|---|
| **Type** | **Description** |
| `https://docs.oracle.com/javase/8/docs/api/java/io/IOException.html` |   |