Nefariis
Nefariis

Reputation: 3549

Confluence WIki Page Not Updating Through API

I trying to use the Rest API to update our wiki page but nothing seems to be happening even though I am receiving 200 codes.

I've tried to accomplish this through both postman as well as Python, and I am receiving the same server response in both cases, but to no avail. Here is my Python code -

curl = 'curl -u user:pass -X POST -H \'Content-Type: application/json\' ' \
           '-d \'{0}\' https://wiki.myCompany.com:8444/confluence/rest/api/content/'\
            .format(json.dumps(new))
    output = subprocess.check_output(['bash', '-c', curl])
    print(output`)

I've tried using both POST and PUT

Here is the response -

PUT https://wiki.myCompany.com:8444/confluence/rest/api/content/
 200 OK       26.47 kB       655 ms
View Request View Response
HEADERS
Content-Encoding: gzip
Content-Length: 6578
Content-Security-Policy: frame-ancestors 'self'
Content-Type: text/html;charset=UTF-8
Date: Wed, 15 Feb 2017 20:24:46 GMT
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=DBCAA4C03DC489A720B8A59D755BD22A; Path=/; Secure; HttpOnly
Vary: User-Agent
X-Accel-Buffering: no
X-Asen: SEN-3386858
X-Ausername: username
X-Confluence-Request-Time: 1487190286413
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-Seraph-Loginreason: OK
X-Xss-Protection: 1; mode=block

BODYview raw


<!DOCTYPE html>
<html>
  <head>
    <title>Dashboard - myCompany Wiki</title>
    <meta http-equiv="X-UA-Compatible" content="IE=EDGE,chrome=IE7">
      <meta charset="UTF-8">
        <meta id="confluence-context-path" name="confluence-context-path" content="">
          <meta id="confluence-base-url" name="confluence-base-url" content="https://wiki.myCompany.com:8444">
            <meta id="atlassian-token" name="atlassian-token" content="abcd227f923fa6d5cce068a25de3bb4a3a3ceca4">
              <script type="text/javascript">
        var contextPath = '';
</script>

..... A lot more html .... but nothing relating to Body or Body.Storage...

My JSON is formed correctly and contains the page ID - Here is the start of it....

{"id":"28870287","type":"page","status":"current","title":"Automated QA Results - Android","body":{"storage":{"value":"<p>These are the results of every git merge...}}

Does anyone have any idea why nothing is happening?

I have tried this through the requests library before - I get the same 200 response code. I was just seeing if you guys notice something wrong with calls themselves rather than the implementation

   # output = requests.post('https://{0}/confluence/rest/api/content'.format(jirasite),
#                     data=(json.dumps(new)),
#                     auth=('user', 'pass'),
#                     headers=({'Content-Type': 'application/json'}))

**** New Update ****

I am trying it from literally the stock curl value which is Given on the Confluence API page

'{"id":"28870287","type":"page","title":"new page","space":{"key":"TST"},"body":{"storage":{"value":"<p>This is the updated text for the new page</p>","representation":"storage"}},"version":{"number":2}}'

Still to no avail... I am at a complete loss....

****** Update Again ******

I am just going to post the code that I am currently working with

    r = requests.get('{0}/rest/api/content/28870287?expand=body.storage,version'.format(conflu_site),
                 auth=(test_user, test_pass)).text

    print(r) # This works as expected

new = '{\"id\":\"28870287\",\"type\":\"page",\"title":\"Automated QA Results - Android\",\"space\":{\"key\":\"TST\"},' \
      '\"body\":{\"storage\":{\"value\":\"<p>This is the updated text for the new page</p>\",' \
      '\"representation\":\"storage\"}},\"version\":{\"number\":2}}'

update_response = requests.put('{0}/confluence/rest/api/content/28870287/'.format(conflu_site),
                                data=new,
                                auth=(test_user, test_pass),
                                headers=({'Content-Type': 'application/json'}))

print("Update Confluence Response: " + str(update_response))

Upvotes: 3

Views: 3998

Answers (2)

Lukas K&#246;rfer
Lukas K&#246;rfer

Reputation: 14493

First of all, please check the base url of your Confluence instance, because by default it looks like xxx.xxx.xxx.xxx:1990/confluence, but one can get rid of the context confluence. In your example, you use wiki.myCompany.com:8444 as something called jirasite and still add confluence before the REST API url rest/api. If you use the right API endpoint, you should never get a HTML response.

Then, please decide which API method you want to use. To create a new content, you have to send a POST request to rest/api/content, but to update an existing content, you have to send a PUT request to rest/api/content/{contentId}.

I just noticed, that you are using the -u option in curl and the auth method of the requests library for authentication. I am not quite sure, but I think it is not possible to authenticate via those techniques. Atlassian lists the possibilities in their documentation and I think, that you have to implement their Basic authentication on your own.

Some additional troubleshooting tips:

Atlassian provides a REST API Browser Plugin to test API requests. If you cannot install plugins in your Confluence instance, you can use a browser extension (e.g. YARC). This way, you can send requests to your Confluence without caring about authentication.

Upvotes: 3

Nefariis
Nefariis

Reputation: 3549

I accepted lukegv's answer as it finally gave me some error messages that I could work with to get the final solution. More importantly, I am posting my script in case anyone comes across this and was infuriated at the lack of documentation of the confluence API.

@lukegv is right - even though the documentation for updating confluence says to use this:

http://localhost:8080/confluence/rest/api/content/3604482

If you are using the wiki site you should be using this

http://localhost:8080/rest/api/content/3604482

The second issue is that you need to increment the version number of the page in order to update. To do that you need to call expand=version like this.

get_json = json.loads(requests.get('{0}/rest/api/content/28870287?expand=body.storage,version'.format(conflu_site),
                 auth=(test_user, test_pass)).text)

Here is how I ended up implementing the code in order for it to work.

# This is the bare minimum that it takes to update a wiki page
update_templete = json.loads('{"id":"28870287","type":"page","body":{"storage":' \
              '{"value":"Test","representation":"storage"}},"version":{"number":2}}')

# This is used to get the current body of the wiki page 
get_json = json.loads(requests.get('{0}/rest/api/content/28870287?expand=body.storage,version'.format(conflu_site),
                 auth=(test_user, test_pass)).text)

# This increments the version number by one
update_templete["version"]["number"] = get_json["version"]["number"] + 1

# This saves the current page's body as a string
body = str(get_json["body"]["storage"]["value"])

# Use this to change the body however you see fit 
new_body = do_Stuff(body) 

# Update the templete's body json with the new body
update_templete["body"]["storage"]["value"] = new_body

# updates the confluence wiki site with the page body
update_response = requests.put('{0}/rest/api/content/28870287/'.format(conflu_site),
                                data=json.dumps(update_templete),
                                auth=(test_user, test_pass),
                                headers=({'Content-Type': 'application/json'}))

Upvotes: 3

Related Questions