Update a page in Confluence using REST API
Asked Answered
G

3

10

This is what I've currently got and it creates a new Confluence page. It doesn't update it. Also it posts it in the root space, TST, but I want it to be in TST/space1/subsection2/updateThisPage.

curl -v -u admin:admin -X POST -H Content-Type: application/json -d  "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage

This is the error message I get

{"statusCode":400,"message":"A page with this title already exists: A page already exists with the title new page in the space with key TST"}

Would it be a permissions error? I know I do not have access to delete.

Grillo answered 19/2, 2015 at 19:19 Comment(0)
A
13

Use request path PUT /rest/api/content/{id}.

This worked for me.

curl -u admin:admin -X PUT -H "Content-Type: application/json" -d "{\"id\":\"26738701\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"RO\"},\"body\":{\"storage\":{\"value\":\"<p>UPDATE This is a new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":2}}" http://localost:10080/rest/api/content/26738701

JSON Payload:

{  
   "id":"26738701",
   "type":"page",
   "title":"new page",
   "space":{  
      "key":"RO"
   },
   "body":{  
      "storage":{  
         "value":"<p>UPDATE This is a new page</p>",
         "representation":"storage"
      }
   },
   "version":{  
      "number":2
   }
}

Don't forget to use:

  • content ID in the data part
  • version number in data part
  • PUT request
  • content ID in the request
Andraandrade answered 20/2, 2015 at 13:25 Comment(4)
Do you know why it moves the page to the root of the hierarchy instead of keeping it where it is?Grillo
@GregWringle, After some trial and error and getting around some annoying quirks, the following worked for me: Use '?expand=ancestors,space,version' in GET request. You will get a list of objects in the 'ancestors' in the response. Use the last object in that list and put it as the only object in the list for 'ancestors' in PUT request. Make sure you have all other required parts such as page ID, version number, etc as before. If you copy the entire value (list of objects) of 'ancestors' from GET response into the PUT request, it won't work!Roup
@Roup Thanks! That works, iaswtw. Also, I found I don't need the whole ancestor object: this suffices: page: { 'id':'2', ... all the other stuff ... ancestors: [ { 'id':'1'} ] That is: I only need the ancestor ID, not the huge big thingy =)Riane
I was missing the "representation"Austine
A
0

Try to use PUT instead of POST.

curl -v -u admin:admin -X PUT -H Content-Type: application/json -d  "{\"id\":\"123456\",\"type\":\"page\",\"title\":\"new page\",\"space\":{\"key\":\"TST\",\"title\":\"updateThisPage\"},\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",\"representation\":\"storage\"}},\"version\":{\"number\":3}}" http://localhost:8090/rest/api/content?spaceKey=TST&title=updateThisPage
Andraandrade answered 19/2, 2015 at 22:20 Comment(1)
That yields {"statusCode":500,"message":"javax.ws.rs.WebApplicationException: null"}Grillo
S
0

If anyone is looking for javascript solution, here is my answer to another question like that Unexpected grunt-http error when posting to Atlassian Confluence api

And here you can find working code i've developed on confluence hackathon https://github.com/devex-web-frontend/dxWebPlugins/blob/master/src/confluence/helpers/buffer.js

Selemas answered 7/5, 2015 at 17:18 Comment(1)
Is there a bash solution, by any chance?Swisher

© 2022 - 2024 — McMap. All rights reserved.