9

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.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
GregWringle
  • 445
  • 3
  • 6
  • 16

3 Answers3

13

Use request /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 data part
  • version number in data part
  • PUT request
  • content ID in request
Eonasdan
  • 7,563
  • 8
  • 55
  • 82
ThePavolC
  • 1,693
  • 1
  • 16
  • 26
  • 3
    Do you know why it moves the page to the root of the hierarchy instead of keeping it where it is? – GregWringle Feb 24 '15 at 20:13
  • 2
    @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! – iaswtw Apr 28 '15 at 16:36
  • @iaswtw 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 =) – Ryan S Aug 27 '15 at 18:06
  • I was missing the "representation" – specimen Mar 22 '21 at 15:21
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
ThePavolC
  • 1,693
  • 1
  • 16
  • 26
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

Community
  • 1
  • 1
Katya Pavlenko
  • 3,303
  • 3
  • 16
  • 28