Updating a webpage using python requests returning error response code 415

543 views Asked by At

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('[email protected]', '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('[email protected]', '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.

1

There are 1 answers

5
KutayAslan On BEST ANSWER

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('[email protected]', 'AIengineering1@ai'))