Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Not able to update the exisiting confluence using Java Rest API

Naveen Kumar D P December 16, 2019

Hi,

We are using confluence cloud instance,

we are trying to edit the existing confluence page using the page ID.I have attached the code below.

public static void main(String[] args) throws JSONException,
AuthenticationException, ClientHandlerException,
ClassNotFoundException, SQLException, ParseException, IOException {
String urlLink = "";
String auth = new String(Base64.encode(""));
String updatePage =
"{\n" +
"\"type\":\"page\",\n" +
"\"ancestors\":\n" +
" [\n" +
" {\n" +
" \"type\":\"page\",\n" +
" \"id\":1501003952\n" +
" }\n" +
" ],\n" +
"\"title\":\"Sprint Macro\",\n" +
"\"space\":\n" +
" {\n" +
" \"key\":\"LAS\"\n" +
" },\n" +
"\"body\":\n" +
" {\n" +
" \"storage\":\n" +
" {\n" +
" \"value\":\"<ac:structured-macro ac:name=\\\"attachments\\\">\n" +
" <ac:parameter ac:name=\\\"old\\\">false<\\/ac:parameter> <ac:parameter ac:name=\\\"patterns\\\">*<\\/ac:parameter> \n" +
" <ac:parameter ac:name=\\\"sortBy\\\">name<\\/ac:parameter> <ac:parameter ac:name=\\\"sortOrder\\\">ascending<\\/ac:parameter> \n" +
" <ac:parameter ac:name=\\\"labels\\\"><\\/ac:parameter> <ac:parameter ac:name=\\\"upload\\\">false<\\/ac:parameter> <\\/ac:structured-macro>\",\n" +
" \"representation\":\"storage\"\n" +
" }\n" +
" }\n" +
"}";
// rest/api/content/
// System.out.println(updatePage);
String Page = invokePostMethod(auth, urlLink ,updatePage);
}

private static String invokePostMethod(String auth, String url, String data)
throws AuthenticationException, ClientHandlerException {
Client client = Client.create();
WebResource webResource = client.resource(url);
ClientResponse response = webResource
.header("Authorization", "Basic " + auth)
.type("application/json").accept("application/json")
.post(ClientResponse.class, data);
System.out.println("Client Responce " + response);
int statusCode = response.getStatus();
System.out.println("Status Code " + statusCode);
if (statusCode == 401) {
throw new AuthenticationException("Invalid Username or Password");
}
return response.getEntity(String.class);
}
}

Please correct me if i missed anywhere in the code.

 

Thanks

Naveen

1 answer

1 accepted

0 votes
Answer accepted
DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 16, 2019

@Naveen Kumar D P  Can you please elaborate on error you are getting?

Naveen Kumar D P December 16, 2019

@DPKJ,

When i run the above command i got the following error,

returned a response status of 500 Internal Server Error
Status Code 500

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 16, 2019

@Naveen Kumar D P  Can you please validate if this curl works for you?

curl -u '<EMAIL>:<TOKEN>' -X PUT -H 'Content-Type: application/json' -d'{"id":"<PAGE_ID>","type":"page","title":"Sample Page","space":{"key":"<SPACE_KEY_OF_PAGE>"},"body":{"storage":{"value":"<p>This is the updated text for the new page</p>","representation":"storage"}},"version":{"number": <PAGE_VERSION_PLUS_ONE>}}' https://<SITE_NAME>.atlassian.net/wiki/rest/api/content/<PAGE_ID>

Few things you can notice here are,

  • I am using PUT request, in place of POST, as PUT should be used when you update a page
  • My request body contain lots of information, you can mimic them.
Like Naveen Kumar D P likes this
Naveen Kumar D P December 16, 2019

Thank you very much @DPKJ  it was working ,

I am able to update the message to the page.

But i am not able to add the JIRA Macro to this page using this curl command.

curl -u '<EMAIL >: <Tokens> -X PUT -H 'Content-Type: application/json' -d'{"id":"1501003952","type":"page","title":"Sprint Macro","space":{"key":"LAS"},"body":{"storage":{"value":"<p /><ac:structured-macro ac:name="jira" ac:schema-version="1" data-layout="full-width" ac:macro-id="fab6f027-6261-4a0c-a326-e38efd4a27a2"><ac:parameter ac:name="server">System JIRA</ac:parameter><ac:parameter ac:name="maximumIssues">20</ac:parameter><ac:parameter ac:name="columns">key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution</ac:parameter><ac:parameter ac:name="jqlQuery">sprint = &quot;2019.11.s2&quot; and status in(Done,Close) and project = &quot;Last Mile&quot; </ac:parameter><ac:parameter ac:name="serverId">***-**-***-***</ac:parameter></ac:structured-macro>
<p />","representation":"storage"}},"version":{"number": "3"}}' https://***/rest/api/content/1501003952

When i tried the above command i got this error.

{"statusCode":500,"message":"org.codehaus.jackson.JsonParseException: Unexpected character ('j' (code 106)): was expecting comma to separate OBJECT entries\n at [Source: com.atlassian.plugins.rest.common.limitrequest.jersey.LimitingRequestFilter$1@7f0896c5; line: 1, column: 143]"}%

 

Could you please help me on this.

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 16, 2019

@Naveen Kumar D P  I was able to add history macro to page this way, you can replace this with other kind of macro.

curl --request PUT \
--url 'https://<SITE>.atlassian.net/wiki/rest/api/content/<PAGE_ID>' \
--user '<EMAIL>:<TOKEN>' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
"id":"<PAGE_ID>",
"type":"page",
"title":"Sample Page",
"space":{"key":"<PAGE_SPACE_KEY>"},
"body":{
"storage":{
"value":"<p>This is the updated text for the new page</p><ac:structured-macro ac:name=\"change-history\" ac:schema-version=\"1\" ac:macro-id=\"360a0d80-082e-4c82-9ee5-0aac5b72f828\" />",
"representation":"storage"
}
},
"version":{"number":<NEXT_VERSION_NUMBER>}
}'

 

Check if this works.

Like Naveen Kumar D P likes this
Naveen Kumar D P December 16, 2019

@DPKJ  Thanks for your help,

I am new to the curl.

In the macro name i used the JIRA name but i am not able to succeed .

curl --request PUT \
--url '***' \
--user '<<:>>' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
"id":"1501003952",
"type":"page",
"title":"Sprint Macro",
"space":{"key":"LAS"},
"body":{
"storage":{
"value":"<p>This is the updated text for the new page</p><ac:structured-macro ac:name=\"jira\" ac:schema-version=\"1\" ac:macro-id=\"fab6f027-6261-4a0c-a326-e38efd4a27a2\">
<ac:parameter ac:name="columns">key,summary,type,created,updated,due,assignee,reporter,priority,status,resolution</ac:parameter>
<ac:parameter ac:name="jqlQuery">sprint = &quot;2019.11.s2&quot; and status in(Done,Close) and project = &quot;Last Mile&quot;
</ac:parameter><ac:parameter ac:name="serverId">3f701a8d-470a-3309-8926-3b7be06fa653</ac:parameter></ac:structured-macro>",
"representation":"storage"
}
},
"version":{"number":3}
}'

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 16, 2019

Let me give it a trial.

Naveen Kumar D P December 17, 2019

Sure . Please let me know if that is working for you!

DPKJ
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 17, 2019

@Naveen Kumar D P  Below worked for me while using Jira Issue Macro,

curl --request PUT \
--url 'https://<SITE>.atlassian.net/wiki/rest/api/content/<PAGE_ID>' \
--user '<EMAIL>:<TOKEN>' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
"id":"<PAGE_ID>",
"type":"page",
"title":"Sample Page",
"space":{"key":"<PAGE_SPACE>"},
"body":{
"storage":{
"value":"<p>This is the updated text for the new page</p><ac:structured-macro ac:name=\"jira\" ac:schema-version=\"1\" ac:macro-id=\"85529c8c-75ad-4bf1-8bc3-da4e55f6d9d7\"><ac:parameter ac:name=\"columns\">key,summary,type</ac:parameter><ac:parameter ac:name=\"jqlQuery\">project=SP</ac:parameter><ac:parameter ac:name=\"server\">System JIRA</ac:parameter><ac:parameter ac:name=\"serverId\">e70747f5-eb31-3622-acda-6395ba8705ce</ac:parameter></ac:structured-macro>",
"representation":"storage"
}
},
"version":{"number":13}
}'

 

I think only difference is that I have escaped value of parameters in macro, you will be easily able to compare this.

Like Naveen Kumar D P likes this
Naveen Kumar D P December 17, 2019

Thank you very much @DPKJ  it was working now.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events