Atlassian Confluence : how do I update page using REST API

eeijlar

I am trying to update a Confluence page using this code: https://bitbucket.org/jaysee00/confluence-rest-api-example/src/master/src/main/java/com/atlassian/api/examples/Main.java

Code is:

public class Confluence {
/**
 * Demonstrates how to update a page using the Conflunence 5.5 REST API.
 */
private static final Logger LOGGER = Logger.getLogger(Confluence.class);;
private static final String BASE_URL = "http://confluence:8080";
private static final String USERNAME = "admin";
private static final String PASSWORD = "admin";
private static final String ENCODING = "utf-8";

private String getContentRestUrl(Long contentId, String[] expansions)
        throws UnsupportedEncodingException {
    String expand = URLEncoder.encode(StringUtils.join(expansions, ","),
            ENCODING);

    return String
            .format("%s/rest/api/content/%s?expand=%s&os_authType=basic&os_username=%s&os_password=%s",
                    BASE_URL, contentId, expand,
                    URLEncoder.encode(USERNAME, ENCODING),
                    URLEncoder.encode(PASSWORD, ENCODING));
}

public void publish() throws ClientProtocolException, IOException,   Exception {
    final long pageId = 36307446;

    HttpClient client = new DefaultHttpClient();

    // Get current page version
    String pageObj = null;
    HttpEntity pageEntity = null;
    try {
        String restUrl = getContentRestUrl(pageId,
                new String[] { "body.storage", "version", "ancestors" });
        HttpGet getPageRequest = new HttpGet(restUrl);
        HttpResponse getPageResponse = client.execute(getPageRequest);
        pageEntity = getPageResponse.getEntity();

        pageObj = IOUtils.toString(pageEntity.getContent());

        LOGGER.info("Get Page Request returned "
                + getPageResponse.getStatusLine().toString());
        LOGGER.info(pageObj);
        LOGGER.info((int)pageObj.trim().charAt(0));
    } finally {
        if (pageEntity != null) {
            EntityUtils.consume(pageEntity);
        }
    }

    // Parse response into JSON
    JSONObject page = new JSONObject(pageObj.trim());

    // Update page
    // The updated value must be Confluence Storage Format
    // NOT HTML.
    page.getJSONObject("body").getJSONObject("storage")
            .put("value", "hello, world");

    int currentVersion = page.getJSONObject("version").getInt("number");
    page.getJSONObject("version").put("number", currentVersion + 1);

    // Send update request
    HttpEntity putPageEntity = null;

    try {
        HttpPut putPageRequest = new HttpPut(getContentRestUrl(pageId,
                new String[] {}));

        StringEntity entity = new StringEntity(page.toString());
        entity.setContentType("application/json");
        putPageRequest.setEntity(entity);

        HttpResponse putPageResponse = client.execute(putPageRequest);
        putPageEntity = putPageResponse.getEntity();

        System.out.println("Put Page Request returned "
                + putPageResponse.getStatusLine().toString());
        System.out.println("");
        System.out.println(IOUtils.toString(putPageEntity.getContent()));
    } finally {
        EntityUtils.consume(putPageEntity);
    }
}

}

The response is alway 'HTTP 404 - Page not found'. I have changed the page id to one I know exists in Confluence.

An exception follows when it tries to parse the response into a JSON object:

avvvaorg.json.JSONException: A JSONObject text must begin with '{' at character 1
at org.json.JSONTokener.syntaxError(JSONTokener.java:496)
at org.json.JSONObject.<init>(JSONObject.java:180)
at org.json.JSONObject.<init>(JSONObject.java:403)
at com.openet.report.publish.Confluence.publish(Confluence.java:74)
at com.openet.report.miner.ReportMiner.generateSummary(ReportMiner.java:268)
at com.openet.report.miner.ReportMiner.runReport(ReportMiner.java:251)
at com.openet.report.miner.ReportMiner.main(ReportMiner.java:138)
eeijlar

Updating confluence pages using REST is not supported by Confluence 4.3.1. The API is much more limited: https://docs.atlassian.com/atlassian-confluence/REST/4.3.1/

You can however update confluence using XML RPC:

public void publish() throws IOException {
    DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
    Date today = Calendar.getInstance().getTime(); 
    XWikiXmlRpcClient rpc = new XWikiXmlRpcClient(CONFLUENCE_URI);
    try {
        rpc.login(USER_NAME, PASSWORD);
        //The info macro would get rendered an info box in the Page
        Page page = new Page();
        page.setSpace("Some space");
        page.setTitle("Testing XML RPC calls in confluence_" + df.format(today));            
        //page.setContent(
        String s = String.format("||Heading 1||Heading 2||Heading 3||%s|col A1|col A2|col A3|", "\r\n");
        page.setContent(s);
        page.setParentId(PAGEID);            
        rpc.storePage(page);
        } catch (XmlRpcException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

            // TODO Auto-generated catch block

}

This requires the following libraries:

import org.apache.xmlrpc.XmlRpcException;
import org.codehaus.swizzle.confluence.Page;
import org.w3c.dom.Document;
import org.xwiki.xmlrpc.XWikiXmlRpcClient;

Note that these libraries are not in the standard maven repository. You will have to update your repository manager (artifactory in my case) to sync with the XWiki maven repo. You will also need the service rocket plugin (https://community.servicerocket.com/servicerocket/topics/the-license-could-not-be-verified-there-is-no-license-certificate-installed-for-customware-scaffolding-plugin-for-confluence) configured correctly on Confluence.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How do I make calls to a REST api using C#?

From Java

How do I create a login API using Django Rest Framework?

From Dev

How do I define an edit page within a REST API?

From Dev

How do I apply a template to a document using Docusign REST API

From Dev

How to upload a file to Atlassian Confluence page using curl

From Dev

How to create new page in Confluence using their REST API?

From Dev

How do I get a list of the Velocity objects and APIs usable in user macros in Atlassian Confluence?

From Dev

How to update a page in Confluence 5.5.1 via rest call

From Dev

How do I deploy a artifact with maven layout using REST API?

From Dev

How to get Atlassian Confluence Space permissions by REST API?

From Dev

How to create a new page in Confluence using jQuery and their REST API?

From Dev

Update a page in Confluence using REST API

From Dev

How do you post a comment to Atlassian confluence using their REST api?

From Dev

Using Confluence REST API to create a table that sorts

From Dev

How do I retrieve my custom variables from a Bamboo Atlassian Build Plan via REST API

From Dev

How do I paste data into a table using Confluence 5.7?

From Dev

In Confluence, how do I create a permanant link to a specific version of a page when that page is the current version?

From Dev

How do I do a git show in Atlassian SourceTree?

From Dev

How to update a page in Confluence 5.5.1 via rest call

From Dev

How do I deploy a artifact with maven layout using REST API?

From Dev

How do I communicate with a REST API using Java/Android?

From Dev

Confluence REST API Authorization issue

From Dev

How do I paste data into a table using Confluence 5.7?

From Dev

How can I update a status for test case in ALM using REST API

From Dev

How to Add Labels to Confluence Page via REST

From Dev

How to create a child page in Confluence by copying an existing page and using the REST API

From Dev

How to export space with comments in Confluence REST API?

From Dev

How to update Atlassian Confluence Wiki using JMeter and the REST API

From Dev

How to create new page as first child of a specific parent in Confluence using their REST API?

Related Related

  1. 1

    How do I make calls to a REST api using C#?

  2. 2

    How do I create a login API using Django Rest Framework?

  3. 3

    How do I define an edit page within a REST API?

  4. 4

    How do I apply a template to a document using Docusign REST API

  5. 5

    How to upload a file to Atlassian Confluence page using curl

  6. 6

    How to create new page in Confluence using their REST API?

  7. 7

    How do I get a list of the Velocity objects and APIs usable in user macros in Atlassian Confluence?

  8. 8

    How to update a page in Confluence 5.5.1 via rest call

  9. 9

    How do I deploy a artifact with maven layout using REST API?

  10. 10

    How to get Atlassian Confluence Space permissions by REST API?

  11. 11

    How to create a new page in Confluence using jQuery and their REST API?

  12. 12

    Update a page in Confluence using REST API

  13. 13

    How do you post a comment to Atlassian confluence using their REST api?

  14. 14

    Using Confluence REST API to create a table that sorts

  15. 15

    How do I retrieve my custom variables from a Bamboo Atlassian Build Plan via REST API

  16. 16

    How do I paste data into a table using Confluence 5.7?

  17. 17

    In Confluence, how do I create a permanant link to a specific version of a page when that page is the current version?

  18. 18

    How do I do a git show in Atlassian SourceTree?

  19. 19

    How to update a page in Confluence 5.5.1 via rest call

  20. 20

    How do I deploy a artifact with maven layout using REST API?

  21. 21

    How do I communicate with a REST API using Java/Android?

  22. 22

    Confluence REST API Authorization issue

  23. 23

    How do I paste data into a table using Confluence 5.7?

  24. 24

    How can I update a status for test case in ALM using REST API

  25. 25

    How to Add Labels to Confluence Page via REST

  26. 26

    How to create a child page in Confluence by copying an existing page and using the REST API

  27. 27

    How to export space with comments in Confluence REST API?

  28. 28

    How to update Atlassian Confluence Wiki using JMeter and the REST API

  29. 29

    How to create new page as first child of a specific parent in Confluence using their REST API?

HotTag

Archive