2021年4月23日星期五

Cannot update webpage using python requests returning error response code 400

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()  

Below is the code I used to update the webpage that is returning error code 400.

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, json=payload, auth=HTTPBasicAuth('svc-Automation@ai.com', 'AIengineering1@ai'))  print (result.content)  

result.content outputs the below status:

{"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"}   

When I go to the link https://confluence.ai.com/rest/api/content/525424594?expand=body.storage I get the rest api content of my page as:

{"id":"525424594","type":"page","status":"current","title":"Update Status","space":{"id":152698959,"key":"AIENG","name":"Consumer Solutions (AI) ENG","type":"global","_links":{"webui":"/display/CSSFW","self":"https://confluence.ai.com/rest/api/space/AIENG"},"_expandable":{"metadata":"","icon":"","description":"","homepage":"/rest/api/content/155503904"}},"history":{"latest":true,"createdBy":{"type":"known","username":"Ashish.Joshi@ai.com","userKey":"8a78e5ac71e441c5017250468bbc0259","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"Ashish.Joshi@ai.com","_links":{"self":"https://confluence.ai.com/rest/api/user?key=8a78e5ac71e441c5017250468bbc0259"},"_expandable":{"status":""}},"createdDate":"2021-04-18T00:14:32.732Z","_links":{"self":"https://confluence.ai.com/rest/api/content/525424594/history"},"_expandable":{"lastUpdated":"","previousVersion":"","contributors":"","nextVersion":""}},"version":{"by":{"type":"known","username":"Ashish.Joshi@ai.com","userKey":"8a78e5ac71e441c5017250468bbc0259","profilePicture":{"path":"/images/icons/profilepics/default.svg","width":48,"height":48,"isDefault":true},"displayName":"Ashish.Joshi@ai.com","_links":{"self":"https://confluence.ai.com/rest/api/user?key=8a78e5ac71e441c5017250468bbc0259"},"_expandable":{"status":""}},"when":"2021-04-18T00:16:18.762Z","message":"","number":1,"minorEdit":false,"hidden":false,"_links":{"self":"https://confluence.wdc.com/rest/experimental/content/525424594/version/1"},"_expandable":{"content":"/rest/api/content/525424594"}},"extensions":{"position":"none"},"_links":{"webui":"/display/CSSFW/Update+Status","edit":"/pages/resumedraft.action?draftId=525424594&draftShareId=0984750c-3696-4918-b9ce-4358fefc26c9","tinyui":"/x/0ldRHw","collection":"/rest/api/content","base":"https://confluence.ai.com","context":"","self":"https://confluence.ai.com/rest/api/content/525424594"},"_expandable":{"container":"/rest/api/space/CSSFW","metadata":"","operations":"","children":"/rest/api/content/525424594/child","restrictions":"/rest/api/content/525424594/restriction/byOperation","ancestors":"","body":"","descendants":"/rest/api/content/525424594/descendant"}}  

Above is the output I get on chrome when this page is not updated. But I want to update its content i.e its storage. I am guessing that the payload I am using right now is not in the right format. Any suggestions? Note: The link, username and password shown here are all fictional.

https://stackoverflow.com/questions/67238370/cannot-update-webpage-using-python-requests-returning-error-response-code-400 April 24, 2021 at 08:49AM

没有评论:

发表评论