3 Replies Latest reply on Mar 9, 2018 2:18 PM by calvinschroeder

    Using Create Document and Create Comment REST services

    daisy

      Being new to using REST services, I was looking for a simple example that used either the Create Document or Create Comment services and I didn't find any.

      Here's a stripped down version (no error handling) of my implementation. Please add comments if there's a better way to do this.

      Hopefully this document will help other newbies.

       

      As per Core REST API here are the two services:

      POST http://domain:port/application_context/api/core/v2/my/private/documents

      POST http://domain:port/application_context/api/core/v2/document/{id}/comments)

       

       

      // variables

      String username = "username";

      String password = "password";

      String authEncoded = Base64.encode( (username+":"+password).getBytes() );

       

      String subjectString = "My document subject";

      String bodyString = "This is the content of my document";

      String postData = "subject=" + URLEncoder.encode(subjectString, "UTF-8") + "&html=" + URLEncoder.encode(bodyString, "UTF-8");

       

      // Create Document

      URL url = new URL("http://jive.app:8080/api/core/v2/my/private/documents";);

      HttpURLConnection connection = (HttpURLConnection) url.openConnection();

      connection.setRequestMethod("POST");

      connection.setDoOutput(true);

      connection.setDoInput(true);

      connection.setAllowUserInteraction(false);

      connection.setRequestProperty("Authorization", "Basic " + authEncoded);

       

      OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

      wr.write(postData);

      wr.flush();

      wr.close();

       

      int code = connection.getResponseCode(); // 201 for success

       

      BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream()));

      String line;

      String response = "";

      while ((line = rd.readLine()) != null )

      {

           response = response + line;

      }

      rd.close();

       

      // Create Comment

      JSONObject json = (JSONObject) JSONSerializer.toJSON(response);

      String docId = json.getString("id");

       

      String commentString = "This is my comment to add to the newly created document.";

      postData = "html=" + URLEncoder.encode(commentString, "UTF-8");

       

      url = new URL("http://jive.app:8080/api/core/v2/document/" + docId + "/comments";);

      connection = (HttpURLConnection) url.openConnection();

      connection.setRequestMethod("POST");

      connection.setDoOutput(true);

      connection.setDoInput(true);

      connection.setAllowUserInteraction(false);

      connection.setRequestProperty("Authorization", "Basic " + authEncoded);

       

      wr = new OutputStreamWriter(connection.getOutputStream(), "UTF-8");

      wr.write(postData);

      wr.flush();

      wr.close();

       

      code = connection.getResponseCode(); // 201 for success

       

      // if you want the URL to the new document.

      String docUrl = ((json.getJSONObject("resources")).getJSONObject("html")).getString("ref");



      I typed the code into this window (no copy and paste) so there might be some compilation errors due to typos.