I´m trying to get any correct response from Confluence REST API, but I´m still getting IOException saying server is answering with 500 status code. Here is the code I´m using:
String test = executePost("http://<server>/rest/api/content/");
public static String executePost(String targetURL)
{
HttpURLConnection connection = null;
try
{
//Create connection
URL url = new URL(targetURL);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json; charset=utf-8");
String name = "admin";
String password = "admin";
String authString = name + ":" + password;
String encodedString = Base64.getEncoder().encodeToString(authString.getBytes());
connection.setRequestProperty("Authorization", "Basic " + encodedString);
connection.setUseCaches(false);
connection.setDoOutput(true);
//Send request
DataOutputStream wr = new DataOutputStream(
connection.getOutputStream());
wr.close();
//Get Response
InputStream is = connection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
StringBuffer response = new StringBuffer();
String line;
while ((line = rd.readLine()) != null)
{
response.append(line);
response.append('\r');
}
rd.close();
return response.toString();
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
finally
{
if (connection != null)
connection.disconnect();
}
}
The error I´m getting back says:
java.io.IOException: Server returned HTTP response code: 500 for URL: http://<server>/rest/api/content/
When i try to send the request over curl with same credentials, I´m getting expected response, which have to mean, I´m missing something in my java code, but I can´t find out what.