8

I use the following code to send a JSON request to my web service, and, for a bad request I return a 400 message code along with a detailed JSON error response in the payload.

I do not understand how I could possibly retrieve this payload on the client side when using HttpURLConnection, as it immediately throws an IOException and the connection's InputStream is null.

HttpURLConnection connection = this.connect(url);
connection.setRequestMethod(method);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(jsonMessage);
InputStream is = null;
try {
    is = connection.getInputStream(); // IOException is thrown, how can I still get payload??
} catch (Exception e) {
} 
String response = getStringFromInputStream(is);

Also tried to use getErrorStream(), but this was containing Apache Tomcat's default error page rather than the payload I can see when, e.g. I use a visual REST API client.

Whymarrh
  • 11,635
  • 13
  • 55
  • 96
user1578796
  • 127
  • 1
  • 1
  • 9
  • sorry but your .... question doesnt make much sense - you're talking about server-to-client conversations via HTTP and yet you show code which apparently describes client-to-server conversations – specializt Jul 11 '14 at 23:01
  • Here's a SO community wiki about HttpURLConnection that might be helpful: http://stackoverflow.com/questions/2793150/how-to-use-java-net-urlconnection-to-fire-and-handle-http-requests – jeffrey_t_b Jul 11 '14 at 23:02
  • getErrorStream() returned you what was really returned. If that's not what you intended to return your Tomcat configuration is wrong somewhere. – user207421 Jul 12 '14 at 00:56
  • @EJP, thanks this is what i thought too, but a visual JSON client actually returns the correct payload, as well as a client i wrote with appache http package: `Status 400 Bad Request Loading time: 25 Response headers Server: Apache-Coyote/1.1 Content-Type: application/json;charset=UTF-8 Transfer-Encoding: chunked Date: Sat, 12 Jul 2014 11:30:57 GMT Connection: close Response { error: "Your email address is already registered! " status: "BAD_REQUEST" fields: [1] 0: "email" - }` – user1578796 Jul 12 '14 at 11:46
  • Whatever the server returned is what you get. Clearly this is different in different cases. The client end has nothing to do with it. You're looking in the wrong place. – user207421 Jul 13 '14 at 01:07
  • I have found numerous threads whereby HTTPUrlConnection simply doesnt transfer the standard payload if the status code is an error message, e.g. http://stackoverflow.com/questions/21526082/get-body-of-bad-request-httpurlconnection-getinputstream, also please refer to blog in case http://www.tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection Either way, thank you all for the help. I ended up using the much more powerfull Apache package (without any server side changes ;-)) – user1578796 Aug 04 '14 at 23:08
  • You haven't found any such thing. At least your cited thread doesn't say so. [This answer](http://stackoverflow.com/a/21529527/207421) provides the solution, which I already gave you above. – user207421 Aug 04 '14 at 23:35

1 Answers1

-2

I have found numerous threads which state that HTTPUrlConnection simply does not transfer the standard payload if the status code is an error message, e.g. stackoverflow.com/questions/21526082/…, also please refer to this blog in case tbray.org/ongoing/When/201x/2012/01/17/HttpURLConnection.

I ended up using the much more powerful Apache package.

Community
  • 1
  • 1
user1578796
  • 127
  • 1
  • 1
  • 9
  • 2
    You haven't found any such thing. At least your cited thread doesn't say so. [This answer](http://stackoverflow.com/a/21529527/207421) provides the solution. – user207421 Aug 04 '14 at 23:34
  • 11
    When using `java.net.HttpURLConnection` and you get an error response code, then you cannot read the body with `getInputStream()` In this case you have to read the body with `getErrorStream()` – Teixi Jan 23 '15 at 10:06
  • @Teixi response should be the accepted one – Pablo Pazos Jun 13 '20 at 23:02