How can I upload HTML to Confluence Page using content type application/json from Jenkins?
Asked by Debraj Bhowmik:

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](/t5/image/serverpage/image-id/123148i0A330E7BB4BD9ED6/image-size/large?v=v2&px=999 "Cerr.jpg")

Please suggest how this issue can be resolved.


Answers:
author: Davin Studer
body: 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).

comments: author: Debraj Bhowmik
body: 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](https://community.atlassian.com/t5/image/serverpage/image-id/123316i5FDB4B3162F013C2/image-size/large?v=v2&px=999 "CJSON.jpg")


author: Davin Studer
body: 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.


author: Debraj Bhowmik
body: 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

<br />

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.


author: Davin Studer
body: 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.


author: Debraj Bhowmik
body: Thanks again. I kept \<br/\> only between two tables and that worked.


Tags: cloud

confluence-cloud