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.