views:

270

answers:

2

I'm using Apache HttpComponents Client to POST to a server that returns JSON. The problem is that if the server returns a 400 error, I seem to have no way of telling what the error was from Java (had to resort to a packet sniffer so far - ridiculous). Here is the code:

HttpClient httpclient = new DefaultHttpClient();
params.add(new BasicNameValuePair("format", "json"));
params.add(new BasicNameValuePair("foo", bar));

HttpPost httppost = new HttpPost(uri);
// this is how you set the body of the POST request
httppost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"));

String responseBody = "";
try {
    // Create a response handler
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    responseBody = httpclient.execute(httppost, responseHandler);
} catch(HttpResponseException e) {
    String error = "unknown error";
    if (e.getStatusCode() == 400) {
        // TODO responseBody and e.detailMessage are null here, 
        // even though packet sniffing may reveal a response like
        // Transfer-Encoding: chunked
        // Content-Type: application/json
        //
        // 42
        // {"error": "You do not have permissions for this operation."}
        error = new JSONObject(responseBody).getString("error");  // won't work
        }
    // e.getMessage() is ""
}

What am I doing wrong? There must be an easy way to get the message of a 400 error. This is elementary.

+1  A: 

responseBody will always be null if an exception is thrown while assigning a value to it.

besides that it's specific behaviour of the implementation - ie Apache HttpClient.

It looks like it doesn't maintain any of the detailed information in the exception (obviously).

I'd load up the source code for HttpClient and debug that.

but first check if there's anything in the e.getCause()...

hope that helps.

pstanton
remember, it's open source - you can always alter it or contribute if you need to.
pstanton
e.getCause() returns null.I know it's open source but I'm a beginner to Java and this is the most basic functionality one can image for an HTTP Client library: give me the error
dandv
glad you found an answer (above)
pstanton
+2  A: 

Why do you use BasicResponseHandler()? The handler is doing that for you. That handler is just an example and shouldn't be used in real code.

You should either write your own handler or call execute without a handler.

For example,

        HttpResponse response = httpClient.execute(request);
        int statusCode = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        responseBody = entity.getContent();

        if (statusCode != 200) {
            // responseBody will have the error response
        }
ZZ Coder
This worked; thanks. All that was left was to slurp the responseBody InputStream into a String.
dandv
You can use EntityUtils.toString(entity) to convert it into string. It handle char-conversion for you. BTW, JSON error should be return with 200. Otherwise, you can't get response from browser.
ZZ Coder