How to edit the page content using rest api
Asked by prasad mavuluru:

Hi All,

I have requirement to update BUILD page which have below fields

BUILD NAME Release Version

test testrealse 1.2

whenever my build completes i need to change the version Number. is it possible to achive using Rest API ?


Answers:
author: Shawn C
body: Hi Prasad,

You can certainly update a Confluence Page by using the [Confluence API](https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/). There are a few different API Calls you'll need to run.  

To pull details about the page you're looking to update, run the following API call:

```
GET <INSTANCE>/rest/api/content/<PAGEID>?expand=body.storage,version
```

This would provide you with the Body Content (Storage) of the page and its current version. You'd then run this call to Update (PUT) the Page:

```
PUT /rest/api/content/<PAGEID>?expand=body.storage
```

With a Payload of:

```
{
 "type":"page",
 "title":"TITLE OF PAGE", 
 "version":{"number":n},
 "body":{
 "storage":{
 "value":"BODY OF PAGE",
 "representation":"storage"
 }
 }
}
```

I hope this helps.

Cheers,  
Shawn

comments: author: prasad mavuluru
body: Hi Shawn,

Thanks for your answer i have tried your solution i am getting response but i dont see page content get updated. Could you please on this any other extra steps i need to do

curl -u username:password-X PUT -H 'Content-Type: application/json' -d'{  
\> "type":"page",  
\> "title":"Deploy History",  
\> "version":{"number":2}},  
\> "body":{  
\> "storage":{  
\> "value":"\<table class=\\"relative-table\\" style=\\"width: 60.8378%;\\"\>\<colgroup\>\<col style=\\"width: 6.67735%;\\" /\>\<col style=\\"width: 21.0114%;\\" /\>\<col style=\\"width: 18.2514%;\\" /\>\<col style=\\"width: 53.7749%;\\" /\>\</colgroup\>\<tbody\>\<tr\>\<th\>Environment\</th\>\<th\>Release Name\</th\>\<th\>Version\</th\>\<th\>Comments\</th\>\</tr\>\<tr\>\<td\>D12\</td\>\<td\>R12B\</td\>\<td\>15.2.155\</td\>\<td\>\<br /\>\</td\>\</tr\>\<tr\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\</tr\>\<tr\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\</tr\>\</tbody\>\</table\>",  
\> "representation":"storage"  
\> }  
\> }  
\> }' http://localhost:8090/rest/api/content/88768864?expand=body.storage

got below response but there is no update to the page

{"id":"88768864","type":"page","status":"current","title":"Deploy History","space":{"id":71696422,"key":"RKB","name":"Knowledge Base","type":"global","_links":{"webui":"/display/RKB","self":"http://localhost:8090/rest/api/space/RKB"},"_expandable":{"metadata":"","icon":"","description":"","homepage":"/rest/api/content/71978725"}},"history":{"latest":true,"createdBy":{"type":"known","username":"DART","userKey":"2c9e829c54438b340154aa1d33c6000a","profilePicture":{"path":"/images/icons/profilepics/default.png","width":48,"height":48,"isDefault":true},"displayName":"Deployment and Run Team","_links":{"self":"http://localhost:8090/rest/experimental/user?key=2c9e829c54438b340154aa1d33c6000a"}},"createdDate":"2018-10-03T11:22:52.000+01:00","_links":{"self":"http://localhost:8090/rest/api/content/88768864/history"},"_expandable":{"lastUpdated":"","previousVersion":"","contributors":"","nextVersion":""}},"version":{"by":{"type":"known","username":"DART","userKey":"2c9e829c54438b340154aa1d33c6000a","profilePicture":{"path":"/images/icons/profilepics/default.png","width":48,"height":48,"isDefault":true},"displayName":"Deployment and Run Team","_links":{"self":"http://localhost:8090/rest/experimental/user?key=2c9e829c54438b340154aa1d33c6000a"}},"when":"2018-10-03T11:22:52.000+01:00","message":"","number":1,"minorEdit":false,"hidden":false,"_links":{"self":"http://localhost:8090/rest/experimental/content/88768864/version/1"},"_expandable":{"content":"/rest/api/content/88768864"}},"ancestors":\[{"id":"71978725","type":"page","status":"current","title":"Knowledge Base","extensions":{"position":"none"},"_links":{"webui":"/display/RKB/Knowledge+Base","tinyui":"/x/5U5KB","self":"http://localhost:8090/rest/api/content/71978725"},"_expandable":{"container":"/rest/api/space/RKB","metadata":"","operations":"","children":"/rest/api/content/71978725/child","history":"/rest/api/content/71978725/history","ancestors":"","body":"","version":"","descendants":"/rest/api/content/71978725/descendant","space":"/rest/api/space/RKB"}}\],"container":{"id":71696422,"key":"RKB","name":"Knowledge Base","type":"global","_links":{"webui":"/display/RKB","self":"http://localhost:8090/rest/api/space/RKB"},"_expandable":{"metadata":"","icon":"","description":"","homepage":"/rest/api/content/71978725"}},"body":{"storage":{"value":"\<table class=\\"relative-table\\" style=\\"width: 60.8378%;\\"\>\<colgroup\>\<col style=\\"width: 6.67735%;\\" /\>\<col style=\\"width: 21.0114%;\\" /\>\<col style=\\"width: 18.2514%;\\" /\>\<col style=\\"width: 53.7749%;\\" /\>\</colgroup\>\<tbody\>\<tr\>\<th\>Environment\</th\>\<th\>Release Name\</th\>\<th\>Version\</th\>\<th\>Comments\</th\>\</tr\>\<tr\>\<td\>D12\</td\>\<td\>R12B\</td\>\<td\>15.2.155\</td\>\<td\>\<br /\>\</td\>\</tr\>\<tr\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\</tr\>\<tr\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\<td\>\<br /\>\</td\>\</tr\>\</tbody\>\</table\>","representation":"storage","_expandable":{"content":"/rest/api/content/88768864"}},"_expandable":{"editor":"","view":"","export_view":"","styled_view":"","anonymous_export_view":""}},"extensions":{"position":"none"},"_links":{"webui":"/display/RKB/Deploy+History","tinyui":"/x/YIFKBQ","collection":"/rest/api/content","base":"http://localhost:8090","context":"","self":"http://localhost:8090/rest/api/content/88768864"},"_expandable":{"metadata":"","operations":"","children":"/rest/api/content/88768864/child","descendants":"/rest/api/content/88768864/descendant"}}


author: Joel Ransom
body: [@Shawn C](/t5/user/viewprofilepage/user-id/502249), I know this thread is a year old, but you omitted what I'm trying to learn, which is how to get the PAGEID in the first place. Let's say you have a page whose title is "Release 09202019" and that page is in some hierarchy, i.e the url is is <https://confluence.domainname.com/display/PJ/Release+-+09192019>

how would you use that info to retrieve the PAGEID for subsequent operations?   

Thanks,

<br />

Joel


author: Christie Finnie
body: I haven't found a way programmatically to get this, buuut if you select the options (... ) menu on the top right of the page and select Page Information. It loads a page with a little meta data (not including the pageid). The url in the address bar on this page contains the pageid instead of the title of the page it is about.

There's a bit here about making an api query for a doc with the exact title if you know it...

<https://developer.atlassian.com/server/confluence/confluence-rest-api-examples/>


author: Joel Ransom
body: Thanks for the reply I should've come back and updated. I'm using this

/rest/api/content?title=ConfluencePageTitle\&spaceKey=SpaceKey

and the Id is in the Results object


author: Christie Finnie
body: Thanks. I see that works.

Also remember to check your permissions. What caused my tests to fail at first was the page I was trying to edit had restrictions enabled that allowed my account access but not the service account that was being used in the script.


author: Manjunath Rajappa
body: Hi [@Shawn C](/t5/user/viewprofilepage/user-id/502249)

I have tried to update a page using all the attributes you have mentioned but Still getting status code 400.
>
> ```
> {
>  "type":"page",
>  "title":"TITLE OF PAGE", 
>  "version":{"number":n},
>  "body":{
>  "storage":{
>  "value":"BODY OF PAGE",
>  "representation":"storage"
>  }
>  }
> }
> ```

When tried to update a page there are no exceptions or error but the status code is 400(bad request). Am I missing any parameters?


author: Christie Finnie
body: You can check if you have invalid chars in your body or are using properties you don't need. Save the return content to your curl command to an html file and view it in a browser. It might have a more specific description of the issue you're having after the 400 error.

When I do this update I increment the doc version, convert my body content to ASCII format, then strip every extra property from the return object.

# Here latestStats.html is the content I want to put in the confluence doc.

$thisPage.body.storage.value = Get-Content -Path "latestStats.html"  
$thisPage.version.number ++  
$thisPage.version.PSObject.properties \| ForEach-Object {  
if ( $_.name -ne "number" ) {  
$thisPage.version.PSObject.properties.remove($_.name) }}

$thisPage \| ConvertTo-Json \| python -m json.tool \| Out-File -Encoding ASCII newPageJson.tmp  
$thisPageJson = Get-Content .\\newPageJson.tmp

curl -k -u $credentials -X PUT -H 'Content-Type: application/json' -d \`@newPageJson.tmp "<https://myconfluence.com/rest/api/content/${pageID>}"


Tags: atl-adm

developer