[ 
https://issues.apache.org/jira/browse/HTTPCLIENT-1756?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

Michael Osipov updated HTTPCLIENT-1756:
---------------------------------------
    Description: 
Hello.  This is my first time posting an issue to this project.  I've done my 
best to be helpful.

Many modern RESTful APIs (e.g. Amazon Web Services) will return a generic 
status description (e.g. "Bad Request") and details in the body of the response 
on an error (e.g. code 400).  

However the org.apache.http.client.fluent.Response.returnContent() ignores the 
body (content) on a return code of over or equal to 300, causing the debugging 
information to be lost.  This leads to generic exception messages.

A possible solution would be to include the body in the exception string, as 
demonstrated in the below ResponseHandler.

{code:java}
    protected static class FaultTolerantContentResponseHandler
            extends ContentResponseHandler {
        @Override
        public Content handleResponse(final HttpResponse response)
                throws HttpResponseException, IOException {
            final StatusLine statusLine = response.getStatusLine();
            final HttpEntity entity = response.getEntity();
            Content content = entity == null ? null : handleEntity(entity);
            if (statusLine.getStatusCode() >= 300) {
                String message = statusLine.getReasonPhrase();
                if (content != null) {
                    message += ", body was " + content.asString();
                }
                throw new HttpResponseException(statusLine.getStatusCode(),
                        message);
            }
            return content;
        }
    }
{code}

Alternatives would be to create a new subclass of HttpResponseException that 
would have an additional getErrorContent() method to preserve backwards 
compatibility.

  was:
Hello.  This is my first time posting an issue to this project.  I've done my 
best to be helpful.

Many modern RESTful APIs (e.g. Amazon Web Services) will return a generic 
status description (e.g. "Bad Request") and details in the body of the response 
on an error (e.g. code 400).  

However the org.apache.http.client.fluent.Response.returnContent() ignores the 
body (content) on a return code of over or equal to 300, causing the debugging 
information to be lost.  This leads to generic exception messages.

A possible solution would be to include the body in the exception string, as 
demonstrated in the below ResponseHandler.

    protected static class FaultTolerantContentResponseHandler
            extends ContentResponseHandler {
        @Override
        public Content handleResponse(final HttpResponse response)
                throws HttpResponseException, IOException {
            final StatusLine statusLine = response.getStatusLine();
            final HttpEntity entity = response.getEntity();
            Content content = entity == null ? null : handleEntity(entity);
            if (statusLine.getStatusCode() >= 300) {
                String message = statusLine.getReasonPhrase();
                if (content != null) {
                    message += ", body was " + content.asString();
                }
                throw new HttpResponseException(statusLine.getStatusCode(),
                        message);
            }
            return content;
        }
    }

Alternatives would be to create a new subclass of HttpResponseException that 
would have an additional getErrorContent() method to preserve backwards 
compatibility.


> Response Body on response codes over 300 should not be ignored
> --------------------------------------------------------------
>
>                 Key: HTTPCLIENT-1756
>                 URL: https://issues.apache.org/jira/browse/HTTPCLIENT-1756
>             Project: HttpComponents HttpClient
>          Issue Type: Improvement
>          Components: HttpClient (classic)
>    Affects Versions: 4.5.2
>            Reporter: Michael Peter Gower
>            Priority: Minor
>             Fix For: Future
>
>
> Hello.  This is my first time posting an issue to this project.  I've done my 
> best to be helpful.
> Many modern RESTful APIs (e.g. Amazon Web Services) will return a generic 
> status description (e.g. "Bad Request") and details in the body of the 
> response on an error (e.g. code 400).  
> However the org.apache.http.client.fluent.Response.returnContent() ignores 
> the body (content) on a return code of over or equal to 300, causing the 
> debugging information to be lost.  This leads to generic exception messages.
> A possible solution would be to include the body in the exception string, as 
> demonstrated in the below ResponseHandler.
> {code:java}
>     protected static class FaultTolerantContentResponseHandler
>             extends ContentResponseHandler {
>         @Override
>         public Content handleResponse(final HttpResponse response)
>                 throws HttpResponseException, IOException {
>             final StatusLine statusLine = response.getStatusLine();
>             final HttpEntity entity = response.getEntity();
>             Content content = entity == null ? null : handleEntity(entity);
>             if (statusLine.getStatusCode() >= 300) {
>                 String message = statusLine.getReasonPhrase();
>                 if (content != null) {
>                     message += ", body was " + content.asString();
>                 }
>                 throw new HttpResponseException(statusLine.getStatusCode(),
>                         message);
>             }
>             return content;
>         }
>     }
> {code}
> Alternatives would be to create a new subclass of HttpResponseException that 
> would have an additional getErrorContent() method to preserve backwards 
> compatibility.



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to