title: How to parse the response body in Java, when the HTTP request has return status 401
body: <p>I am consuming a RESTful JSON API using Spring's <code>RestTemplate</code> and Jackson. In some cases we may receive a <code>Status 401</code> (Unauthorized) response with a custom JSON body, that is defined by the API manufacturer, and looks like this:</p>
  
  <pre><code>{
      "code": 123,
      "message": "Reason for the error"
  }
  </code></pre>
  
  <p>We need to parse the body, and use the <code>code</code> property in our business logic.</p>
  
  <p>This is the error response Java object we need to parse to:</p>
  
  <pre><code>public class CustomError {
  
      @JsonProperty
      private Integer code;
      @JsonProperty
      private String message;
  
      public Integer getCode() {
         return code;
      }
      public String getMessage() {
          return message;
      }
  }
  </code></pre>
  
  <p>And a custom error handler to do this:</p>
  
  <pre><code>public class CustomErrorHandler extends DefaultResponseErrorHandler {
      private RestTemplate restTemplate;
      private ObjectMapper objectMapper;
      private MappingJacksonHttpMessageConverter messageConverter;
  
  
      @Override
      public boolean hasError(ClientHttpResponse response) throws IOException {
          return super.hasError(response);
      }
  
      @Override
      public void handleError(final ClientHttpResponse response) throws IOException {
  
          try {
              CustomError error = 
                  (CustomError) messageConverter.read(CustomError.class, response);
              throw new CustomErrorIOException(error, error.getMessage());
          } catch (Exception e) {
              // parsing failed, resort to default behavior
              super.handleError(response);
          }
      }
  }
  </code></pre>
  
  <p>The error handler fails with an <code>HttpMessageNotReadableException</code> in the try block:</p>
  
  <blockquote>
    <p>"Could not read JSON: cannot retry due to server authentication, in streaming mode"</p>
  </blockquote>
  
  <p>This is how I am sending requests:</p>
  
  <pre><code>restTemplate.postForObject(url, pojoInstance, responseClass);
  </code></pre>
  
  <p>If the same request is executed with a plain old rest client program, like Postman, the expected JSON response is received. So, I assume the problem could be with the Spring's <code>ClientHttpResponse</code> implementation somehow not allowing access to the response body, in case of the 401 status.</p>
  
  <p>Is it indeed possible to parse the response body? </p>
  
  <p><strong>Update</strong></p>
  
  <p>From what I investigated, the <code>RestTemplate</code> class uses <code>ClientHttpResponse</code> which in turn creates an <code>sun.net.www.protocol.http.HttpURLConnection</code> that provides the input stream. It is there, where the input stream is being neglected and an <code>IOException</code> is thrown:</p>
  
  <blockquote>
    <p>cannot retry due to server authentication, in streaming mode</p>
  </blockquote>
  
  <p>So, the <code>HttpURLConnection</code>'s implementation is causing the issue.</p>
  
  <p>Will it be possible to avoid this problem? Perhaps we should use an alternative implementation that does not ignore the response body in case of an error status code? Can you recommend any alternatives?</p>