0

I am trying to update an already existing page in Atlassian confluence page through the Python requests module. I am using the requests.put() method to send the http request to update my page. The page already has the title "Update Status". I am trying to enter one line as the content of the page. The page id and other information that is within the json payload has been copied by me directly from the rest/api/content... output of the webpage I am trying to access. Note: I am already able to access information from the webpage through python requests.get but I am not able to post information to the webpage.

Method used to access information from the webpage which works:

response = requests.get('https://confluence.ai.com/rest/api/content/525424594?expand=body.storage',
                        auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai')).json()

Method used to update information to that page which does not work as the response is in the form of an error 415.

import requests
from requests.auth import HTTPBasicAuth
import json

url = "https://confluence.ai.com/rest/api/content/525424594"
payload =  {"id":"525424594","type":"page", "title":"new page-Update Status","space":{"key":"TST"},"body":{"storage":{"value": "<p>This is the updated text for the new page</p>","representation":"storage"}}, "version":{"number":2}}

result = requests.put(url, data=payload, auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
print (result)

I am guessing that the payload is not in the right format. Any suggestions? Note: The link, username and password shown here are all fictional.

Ashish
  • 75
  • 1
  • 9

1 Answers1

0

Try sending the data with the "json" named argument instead of "data", so requests module would set the application/json to content-type header.

result = requests.put(url, json=payload, auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))
KutayAslan
  • 494
  • 6
  • 13
  • Does not work as I get a new error response 400 – Ashish Apr 23 '21 at 20:07
  • 1
    @Ashish It depends on the API implementation but by convention if you get 400 error instead of 415 that means you've solved the content-type issue. Can you share the result.content with me? Because that new error you had is probably caused of the payload (you sent) not being matched with the format that API is expecting. API might have sent you a response mentioning the format issues in your payload. https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400 – KutayAslan Apr 23 '21 at 20:13
  • Sure the content mentions: {"statusCode":400, "data":{"authorized":true, "valid":false, "allowedInReadOnlyMode":true, "errors can't change an existing page's space.", "args":[]}}], "successful":false}, "message":"Could not update content of type : class com.atlassian.confluence.pages.Page with id 525424594","reason": "Bad Request"} – Ashish Apr 23 '21 at 20:26
  • Shouldn't the format be same as the one that gets shown when I got to the link https://confluence.ai.com/rest/api/content/525424594. So there I just update the storage value which should update the body of my webpage but its saying Bad Request which I do not get. – Ashish Apr 23 '21 at 21:18
  • This is another issue than the original question asks for. I think you should mark this answer as accepted answer since you're no longer getting 415 error. The follow-up questions deserve their own question post https://meta.stackoverflow.com/questions/266767/what-is-the-the-best-way-to-ask-follow-up-questions – KutayAslan Apr 23 '21 at 21:33