LoginSignup
5
7

More than 5 years have passed since last update.

Pythonでconfluenceに新規ページを作る

Last updated at Posted at 2016-12-07

業務でコンフルを使っているのですが、ページの作成をどうにか自動で出来ないかと思い、APIを探してみたらあったのでさっそく使ってみました。
以下にAPIのサンプルがあるので、curlですがすぐに試すことができます。
Confluence REST API Examples
リファレンスはこちら

curlで実行してみる

まずはサンプルにある通りcurlで作ってみる。

curl -u username:password -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://your_confluence_domain/rest/api/content/

どこかの子ページとして作りたい場合はこっち

curl -u username:password -X POST -H 'Content-Type: application/json' -d'{"type":"page","title":"new page", "ancestors":[{"id":1234}], "space":{"key":"TST"},"body":{"storage":{"value":"<p>This is a new page</p>","representation":"storage"}}}' http://your_confluence_domain/rest/api/content/

どうやら"ancestors":[{"id":1234}]で親ページを指定している模様。

Pythonで新規ページを作る

指定するパラメータはcurlのときと同様。
※一部抜粋

import requests
def main():
  payload = {
    'type': 'page',
    'title': 'new page',
    'space': {
      'key': 'TST'
    },
    'ancestors': [{'id': 1234}],
    'body': {
      'storage': {
        'value': '<p>This is a new page</p>,
        'representation': "storage"
      }
    }
  }

  headers = {
    'Content-Type': 'application/json',
    'Authorization': 'Basic ' + base64.b64encode('username:password')
  }

  url = 'http://your_confluence_domain/rest/api/content/'

  response = requests.post(url, data = json.dumps(reqdata), headers = headers)
  response.raise_for_status()
5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7