How to create new page in Confluence using their REST API? [closed]
Asked Answered
B

2

27

I need working example of creating new wiki page in confluence using rest api. I prefer the new page to be created under specific space and specific page. I read their api documentation and looked at few examples they had and still coming short.

Here is an example they had on their site

curl -u admin:admin -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://localhost:8080/confluence/rest/api/content/ | python -mjson.tool

I tried above with my space name, new title and changed the url to mysite/rest/api/content and returned content was basically html page saying the page does not exist or the page exists but you do not have permission. I have confirmed that I have access to confluence wiki and can create new wiki using my credentials.

What is also not clear is in the example above how is it calling the specific api that creates the page? It does not make sense.

similar question was asked on their forum, but no reasonable answer https://answers.atlassian.com/questions/149561/simple-confluence-rest-api-usage-what-am-i-missing

(I guess my end goal is to be able to create new wiki page on confluence automatically) I am okay to give up on confluence REST API to other solution if necessary.

Blizzard answered 7/5, 2014 at 16:49 Comment(1)
Old but valid question, here is their specification: developer.atlassian.com/cloud/confluence/rest/… There are examples on several languages as well, from curl, nodejs etc.Frausto
A
31

My suspicion is that you are not using a new-enough version of Confluence. The REST API for creating a new page was introduced in Confluence 5.5 (which came out 8 days ago). The API documentation is versioned, and you should always use the version corresponding to your Confluence release. The 5.5 API docs include the page creation API you need, but older versions do not. You can change the version in the above URL to get the API version matching your Confluence release.

Confluence 5.4 and prior also used a different root prefix for the REST API (/rest/prototype/1/content) which is one possible reason for getting a page not found error.

The example on the Atlassian site is also confusing because it includes an extra "/confluence" in the URL, which you would only need if Confluence were set up with a context path. This could also result in a page not found error if you were using Confluence 5.5+ (although your post suggests that you already corrected for this).

Additionally, you need to tell Confluence that you are using the basic authentication method by adding a special os_authType query parameter.

The following example works for me on Confluence 5.5 (don't forget to change the port and space key as appropriate).

For safety, I also added the appropriate content type to the Accept header, although this seems to be not needed in practice.

curl -v -u admin:admin -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d'{"type":"page","title":"new page","space":{"key":"ATTACH"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' "http://localhost:8090/rest/api/content/?os_authType=basic"

To answer your last question, the specific API that creates the page is determined by the URL itself and the request method. For example, performing a GET on "/rest/api/content" will fetch an existing page (given the appropriate query parameters), while performing a POST will create a new page.

EDITED TO ADD:

See also my comment below for how to create a page as a child of another existing page, as opposed to just at the top level of a space.

Atli answered 7/5, 2014 at 19:14 Comment(5)
Thank you for a quick reply. Your answer clears up a lot of questions I had. Yes I was using older version of Confluence. I also did not enable remote API setting on Confluence. After enabling that option and using 5.5 makes your suggested solution work. Now I might be asking too much but, With this new rest api can I use file instead of passing in raw html in the command. Also can you specify parent pages id? The documentation did not have it. Maybe because it is only 8 days old :)Blizzard
Enabling the Remote API is good advice too. This is totally undocumented (YMMV with future versions), but you can specify the parent page through the "ancestors" property in 5.5. This works for me to create a child of page #1048582: curl -v -u admin:admin -X POST -H 'Content-Type: application/json' -H 'Accept: application/json' -d'{"type":"page","ancestors":[{"type":"page","id":1048582}],"title":"third new child page","space":{"key":"ATTACH"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' "http://localhost:8090/rest/api/content/?os_authType=basic"Atli
I tried above and it works. Now I can create a new page under any other page. I also tweaked curl setting little bit, so it can read the new page content from a file by using "-d @filename.json". This can come in handy when your new page can contain a lot of content.Blizzard
I haven't managed to gut the update functionality to work properly. When updating an existing nested page, it will either go orphan (using example curl provided by Confluence API documentation) or it will become a child of the space's home page. Logged an issue with Atlassian CR-487Sutter
Is it possible to use this API to create pages on an Atlassian-hosted Confluence instance?Pyle
R
1

Not REST api, but a work around I put together. Try this:

To move a page as child page

curl -X GET \
'<your-confluence-URL>/pages/movepage.action?pageId=<page-to-be-moved-pageId>&spaceKey=<target-space-key>&targetTitle=<target-title-of-parent-page>&position=append' \
-H 'authorization: Basic <encoded-username-password>' \ 
-H 'x-atlassian-token: no-check'

To move a page as top level page in space

curl -X GET \
'<your-confluence-base-URL>/pages/movepage.action?pageId=<page-to-be-moved-pageId>&spaceKey=<target-space-key>&position=topLevel' \...
Rooney answered 19/5, 2017 at 22:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.