How can I upload HTML to Confluence Page using content type application/json from Jenkins?

Debraj Bhowmik December 21, 2020

I am trying to update an existing Confluence Page from Jenkins. I am using the below command:

curl -u username:password -X PUT -H 'Content-Type: application/json' -d @update.json https://myapp.net/confluence/rest/api/content/${PAGE_ID} | python -mjson.tool.

 

The update.json has the below content:

'{"id":"3884567","type":"page","title":"JenkinsBuildStatus","space":{"key":"FDRATES"},"body":{"storage":{"value":"<table><tr><td>Hello</td><td>World</td></tr></table>","representation":"storage"}},"version":{"number":"2"}}'

 

The update of page using the curl command fails with the below error:

{
    "message": "",
    "reason": "Internal Server Error",
    "statusCode": 500
}

It seems the error is due to the '<' and '>' characters inside the body of update.json. 

I put backslash \ in front of '<' and '>' like below and that also failed with the same Internal Server Error 500:
 '{"id":"3884567","type":"page","title":"JenkinsBuildStatus","space":{"key":"FDRATES"},"body":{"storage":{"value":"\<table\>\<tr\>\<td\>Hello\</td\>\<td\>World\</td\>\</tr\>\</table\>","representation":"storage"}},"version":{"number":"2"}}'

I changed the '<' and '>' with '&lt;' and '&gt;' respectively:

'{"id":"3884567","type":"page","title":"JenkinsBuildStatus","space":{"key":"FDRATES"},"body":{"storage":{"value":"&lt;table&gt;&lt;tr&gt;&lt;td&gt;Hello&lt;/td&gt;&lt;td&gt;World&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;","representation":"storage"}},"version":{"number":"2"}}'

 

With these substitutions the curl command could update the page, but in the page the content is not showing as table, instead the page is showing the HTML as text.

Cerr.jpg

Please suggest how this issue can be resolved.

 

1 answer

1 accepted

0 votes
Answer accepted
Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 22, 2020

Couple things that might be it. In your update.json is there a reason all your JSON is inside single quotes? I don't think it should be that way. Also, when I do this I do not make the version number a string. I leave it as a number (unquoted).

Debraj Bhowmik December 22, 2020

Thanks for your suggestions. I reexamined my code. The update.json contents are not inside single quotes and the version number is not a String, it is an Interger.

CJSON.jpg

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 23, 2020

I don't think you need to escape out the < and > part of the elements. Things that need to be escaped in JSON are ", \, new line, carriage return, and tab.

Like Debraj Bhowmik likes this
Debraj Bhowmik December 24, 2020

Thanks for your suggestion! The < and > symbols without \ (escape) worked.

Actually the root cause was the <br> tag that I added between tables. The update failed with the below error as Confluence Page expected a closing </br> tag:

"message": "Error parsing xhtml: Unexpected close tag </xml>; expected </br>.\n at [row,col {unknown-source}]: [1,1343]",
"reason": "Bad Request",
"statusCode": 400


So I removed the <br> tag and the update succeeded.
Also I tried after adding a <br> as the opening tag and </br> as the closing tag, and the update succeeded.
I tried with adding a <br> as the opening tag and <br/> as the closing tag, and the update failed with same "statusCode": 400 error.

Davin Studer
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
December 24, 2020

Tables are block level elements anyway, so you should not need a <br>. It is odd that <br/> didn't work as that is how line breaks are stored in Confluence's storage format. Glad it's working though.

Like Debraj Bhowmik likes this
Debraj Bhowmik December 28, 2020

Thanks again. I kept <br/> only between two tables and that worked. 

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
FREE
PERMISSIONS LEVEL
Product Admin
TAGS
AUG Leaders

Atlassian Community Events