0

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.

Mono
  • 206
  • 1
  • 21
  • The problem is that the code you have posted is the client code while status 500 means server error. – Bhesh Gurung Jun 05 '18 at 15:23
  • Try to log the HTTP request sent from your Java code as well as CURL, and make a comparison of the two requests. – Bhesh Gurung Jun 05 '18 at 15:26
  • Bhesh Gurang: Log shows no info on that error. I guess I need to make the log more verbose and try it again. – Mono Jun 06 '18 at 14:05
  • I don't mean the error log. What I mean is printing the HTTP request sent by Java (like [this](https://stackoverflow.com/q/3428341/738746)) and comparing it to the HTTP request sent by CURL. That might be helpful since you said that the CURL request is working. – Bhesh Gurung Jun 06 '18 at 14:35
  • Ok, I couldn´t get this logging working, but I tried to set exactly the same headers from java as was in working browser request and I´m still getting that error. Server side log would really help. – Mono Jun 07 '18 at 15:04

1 Answers1

0

Doesn't look like you are sending anything.
You should send Json string in ur DataOutputStream like:

DataOutputStream wr = new DataOutputStream (
                connection.getOutputStream());
        wr.writeBytes(json);
        wr.close();

where the json string should be something like with respect to available space and title in your confluence server:

{"type":"page","title":"new page",
"space":{"key":"<YOUR SPACE KEY>"},"body":{"storage":{"value":"<p>This is <br/> a new page</p>","representation":
"storage"}}}
Elazaron
  • 476
  • 11
  • 21