Reputation: 261
I'm writing an application that will migrate a JBoss Guvnor packages using its REST API. Basically it reads all the assets through REST GET requests and writes them to another Guvnor using PUT/POST requests.
Here's a little snippet of my method that does POSTs:
HttpURLConnection conn = (HttpURLConnection) path.getURL()
.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-type", path.getType().getAcceptMIME());
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestProperty("Authorization", "Basic " + authString);
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
conn.getOutputStream()));
bw.write(content);
bw.flush();
conn.connect();
It works, all the assets are being migrated, but every time conn.getResponseCode()
returns 500 and conn.getResponseMessage()
returns Internal Server Error.
I'm fairly new to working HTTP requests with java and I don't know if this should be happening, why is it happening and if it's due to some fault in my snippet, or is it server-side (Guvnors) bug?
Upvotes: 0
Views: 3163
Reputation: 719551
There's nothing obviously wrong with your code. A 500 response means (should mean) that the server has encountered an internal problem. It is possible that something unusual about your requests is triggering the problem, but it could also be something totally unrelated.
I suggest that you do the following:
Check the server-side logs for errors / exceptions that coincide with the failed requests.
Open, read and log the HttpUrlConnection's error stream. It may well have a more informative error message.
Upvotes: 1
Reputation: 115388
Your client side code looks OK. The standard HttpUrlConnection always throws exception when trying to read from input stream when the response code is not 2xx series. Response code 500 means that unexpected error happened at server side. Refer to the server logs. You will find the cause there.
Good luck.
Upvotes: 1