132

I have problem with retrieving Json response in case when server returns error. See details below.

How I perform the request

I use java.net.HttpURLConnection. I setup request properties, then I do:

conn = (HttpURLConnection) url.openConnection();

After that, when request is successful, I get response Json:

br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
sb = new StringBuilder();
String output;
while ((output = br.readLine()) != null) {
  sb.append(output);
}
return sb.toString();

... and the problem is:

I can't retrieve Json received when the server returns some error like 50x or 40x,. Following line throws IOException:

br = new BufferedReader(new InputStreamReader((conn.getInputStream())));
// throws java.io.IOException: Server returned HTTP response code: 401 for URL: www.example.com

The server sends body for sure, I see it in external tool Burp Suite:

HTTP/1.1 401 Unauthorized

{"type":"AuthApiException","message":"AuthApiException","errors":[{"field":"email","message":"Invalid username and/or password."}]}

I can get response message (i.e. "Internal Server Error") and code (i.e. "500") using following methods:

conn.getResponseMessage();
conn.getResponseCode();

But I can't retrieve request body... maybe there is some method I didn't notice in the library?

Roland Tepp
  • 8,301
  • 11
  • 55
  • 73
kiedysktos
  • 3,910
  • 7
  • 31
  • 40

3 Answers3

154

If the response code isn't 200 or 2xx, use getErrorStream() instead of getInputStream().

user207421
  • 305,947
  • 44
  • 307
  • 483
107

Wrong method was used for errors, here is the working code:

BufferedReader br = null;
if (100 <= conn.getResponseCode() && conn.getResponseCode() <= 399) {
    br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
} else {
    br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
}
kiedysktos
  • 3,910
  • 7
  • 31
  • 40
  • 3
    `HttpURLConnection.getErrorStream()` (`sun.net.www.protocol.http` implementation) returns `null` unless `responseCode >= 400`, so your check for 299 is probably incorrect. – vladr Mar 16 '18 at 14:46
  • 15
    If you are using Java 8, you can get the response as a String. `String responseBody = br.lines().collect(Collectors.joining());` – Lanil Marasinghe Apr 17 '19 at 16:56
  • 1
    The correct range is 200...399, as 3xx is a "redirect" status, not an error. – luca.vercelli Jun 05 '20 at 10:26
  • 1
    @luca.vercelli I think it should be 100..399 because 100..199 is for "Informational" responses as defined in https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#1xx_informational_response – WindRider Oct 09 '20 at 17:21
14

This is an easy way to get a successful response from the server like PHP echo otherwise an error message.

BufferedReader br = null;
if (conn.getResponseCode() == 200) {
    br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String strCurrentLine;
        while ((strCurrentLine = br.readLine()) != null) {
               System.out.println(strCurrentLine);
        }
} else {
    br = new BufferedReader(new InputStreamReader(conn.getErrorStream()));
    String strCurrentLine;
        while ((strCurrentLine = br.readLine()) != null) {
               System.out.println(strCurrentLine);
        }
}
Sharhabeel Hamdan
  • 1,273
  • 13
  • 15