Simple Python example for creating a new page using Rest API?

I've been searching for days and am finding page after page of useless information.

Searches for Python examples return examples in CURL, VB, javascript, and others but nothing that I can actually make work in Python.

I simply need to create a page and supply about 30K of auto-generated XHTML or Wiki-markup data to it.

This is a long-term project so I do not want to fall back to the old APIs that will be retired shortly.
Can anyone point to an example or post something to get me started? 


Thanks!

5 answers

1 vote
Stephen Deutsch Community Champion Mar 11, 2015

Hi Tony,

I was also curious what would be the simplest possible thing that could work.  I usually just use the XML-RPC api, but it would be nice to know how to use the REST api too, and I finally managed to get it going. Here you go:

import urllib2
import base64
conf_serverurl = "http://my-confluence:1990/confluence"
username = "admin"
password = "admin"
stringToEncode = username + ":" + password
encodedString = base64.b64encode(stringToEncode)
url = conf_serverurl + "/rest/api/content?os_username=" + username + "&os_password=" + password
data = '{"type":"page","ancestors":[{"type":"page","id":98334}],"title":"new page","space":{"key":"ds"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}'
headers = { 'Authentication': 'Basic ' + encodedString, 'Content-type': 'application/json', 'Accept': 'application/json', 'X-Atlassian-Token': 'no-check' }
req = urllib2.Request(url, data, headers=headers)
response = urllib2.urlopen(req)
data = response.read()
print data

To choose which page will be the parent, just put the page id where it says 98334, put in the proper space key, and put the xhtml markup code where it says "<p>this is a new page</p>".  Everything else should be pretty self-explanatory.

Or if you don't want to do programming, you can use the Confluence Command Line Interface (CLI).

Thanks. That would look like a great option.

Although adding more software to support what should be only a very small part of a very large end-to-end process is not optimal. Let alone the fact that it is highly unlikely that I would get approval for installing anything on our server. But I will keep a link to that, should it prove impossible to get this done with the built-in capabilities.


Hmm, didn't work for me.

I edited an existing page and got its ID (although I would eventually like to create a new page). I noticed that the code does not specify a POST v.s. GET. Is that not necessary? (i.e. is POST assumed?)

Yes, I did change the serverurl, username and password as well... :-)

I get the following error with the above:
Traceback (most recent call last):
  File "post_test.py", line 12, in &lt;module&gt;
    response = urllib2.urlopen(req)
  File "C:\Python27\lib\urllib2.py", line 127, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 442, in error
    result = self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 629, in http_error_302
    return self.parent.open(new, timeout=req.timeout)
  File "C:\Python27\lib\urllib2.py", line 410, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 523, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 448, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 382, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 531, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
urllib2.HTTPError: HTTP Error 404: Not Found
0 vote
Stephen Deutsch Community Champion Mar 17, 2015

Hi Tony,

The code above is supposed to create a new page.  The page ID that is in the request is for which page will be the parent for the new page, not which page is edited.

Also, you are right that the post is implicit; whenever you do a call to urllib2.Request which has data, it automatically sends it as a POST.

My guess is that either you put the wrong URL for the server URL (it should match the base URL and context path for your instance), either that or you are running Confluence version less than 5.5.  Confluence 5.5 is the first version to have the new REST API.

You can use this slightly modified version of the above code to get a better error message:

import urllib2
import base64
conf_serverurl = "http://my-confluence.mycompany.com"
username = "admin"
password = "admin"
stringToEncode = username + ":" + password
encodedString = base64.b64encode(stringToEncode)
url = conf_serverurl + "/rest/api/content?os_username=" + username + "&amp;os_password=" + password
data = '{"type":"page","ancestors":[{"type":"page","id":98334}],"title":"new page","space":{"key":"ds"},"body":{"storage":{"value":"&lt;p&gt;This is a new page&lt;/p&gt;","representation":"storage"}}}'
headers = { 'Authentication': 'Basic ' + encodedString, 'Content-type': 'application/json', 'Accept': 'application/json', 'X-Atlassian-Token': 'no-check' }
req = urllib2.Request(url, data, headers=headers)
try:
    response = urllib2.urlopen(req)
    data = response.read()
except urllib2.HTTPError, error:
    data = error.read()
print data

I tried the same. But I get the following error. {"statusCode":403,"data":{"authorized":false,"valid":true,"errors":[]},"message":"Could not create content with type page"}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Posted Jul 10, 2018 in Confluence

We want to see the templates you've created in Confluence!

Hi Community, Jessica here from the Confluence Product Marketing team!  July’s community challenge is all about sharing pictures  — and as an extension of our first post on what ...

667 views 21 11
Join discussion

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you