Confluence REST API 500 server error using Python requests Lib

I am attempting to automate the update of Confluence pages using a Python 3.4 script. I first tested using the following cURL:

curl -E jchittum -X PUT -H 'Content-Type: application/json' -d '{"id": "48179230","type": "page","title": "Confluence Api Test","body": {"storage": {"value": "<p> New Content </p>","representation": "storage"}},"ancestors":[{"id": "4595907","type": "page","title": "Luna Release"}],"version": {"number": 5}}' https://ouratlassianserver.com/wiki/rest/api/content/48179230 | python -mjson.tool 

This requests updated just fine, and returned the proper JSON.

When I tried to replicate it Python, I get:

{'message': 'javax.ws.rs.WebApplicationException: null', 'statusCode': 500}

When doing a GET operation, everything works fine. Only seeing this on a PUT. We use SSL for authentication

The Python 3.4 code is:

import requests

import json

confPutAddress = "https://ouratlassianserver.com/wiki/rest/api/content/48179230" 

cert = "/path/to/cert.pem"
certKey = "path/to/cert.key"

postDict =

{
"id": "48179230",
"type": "page",
"title": "Confluence Api Test",
"version": {
    "number": 6
},
"ancestors": [
     {
    "id": "4595907" 

    }
],
"type": "page",
"body": {
    "storage": {
    "value": "<p>New page data.</p>",
    "representation": "storage"
    }
},
"space": {
    'id': 1179650,
    'name': 'Luna',
    'type': 'global',
    }
}

confPUT = requests.put(postAddress, data = postDict, verify=True, cert = (cert, certKey))

print(confPUT.text)


I added space after seeing the following page: https://answers.atlassian.com/questions/38379050
 

1 answer

1 accepted

figured out my errors with the help of my team: 

1) convert dict to json: convDict = json.dumps(postDict)

postJSON = json.loads(convDict)

2) add headers:

headers = { "content-type" : "application/json" }

 3) set requests.put to use JSON

confPUT = requests.put(postAddress, headers = headers, json = postJSON , verify=True, cert = (cert, certKey))

 

Suggest an answer

Log in or Sign up to answer
Atlassian Community Anniversary

Happy Anniversary, Atlassian Community!

This community is celebrating its one-year anniversary and Atlassian co-founder Mike Cannon-Brookes has all the feels.

Read more
Community showcase
Posted yesterday in Jira

We want to know what Jira Service Desk apps you're using!

Hi Community! My name is Amir and I’m on the Jira Service Desk product marketing team at Atlassian. Our team would love to understand how you’re leveraging our ecosystem for Jira Service Desk. Wha...

40 views 0 5
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