Hello guys.
I need to update a page using REST API and Python, but I can not do it. I get these errors:
DEBUG :09/08/2023 12:07:01 Starting new HTTP connection (1): confluence.xxx.net:80
DEBUG :09/08/2023 12:07:01 http://confluence.xxx.net:80 "PUT /rest/api/content/534283271 HTTP/1.1" 301 169
DEBUG :09/08/2023 12:07:01 Starting new HTTPS connection (1): confluence.xxx.net:443
DEBUG :09/08/2023 12:07:01 https://confluence.xxx.net:443 "PUT /rest/api/content/534283271 HTTP/1.1" 415 0
This is what I do:
url = "http://confluence.xxx.net/rest/api/content/534283271"
# New content for the page
new_content = """ <p>This is the updated content for the page.</p> """
payload = {'username': 'bob', 'email': 'bob@bob.com'}
try:
# Make the API request to update the page
response = requests.put(url,
auth = HTTPDigestAuth(dConnection.get('username'),
dConnection.get('password')),
headers = {"Content-Type": "application/json"},
data = payload,
verify = False
)
...
...
Tried different options like changing headers, data content, etc without success.
Note: I can read from Confluence using REST API without issues.
Perhaps someone can show me the light š
Regards
Javier
Hi, @Javier Lopez
As I see, you're trying to connect to http resource, it's 80 port, then your request is redirected (301 status code) to https resource, 443 port.
After that you're receiving error (415 code) that payload is invalid. Looks like you need to check body of request.
Usually request, to update body, looks like:
Address:
PUT /rest/api/content/<PAGEID>?expand=body.storage
Payload:
{
"type":"page",
"title":"TITLE OF PAGE",
"version":{"number":n},
"body":{
"storage":{
"value":"BODY OF PAGE",
"representation":"storage"
}
}
}
Oh, and if you're using https to access service, change url variable, to
url = "https://confluence.xxx.net/rest/api/content/534283271?expand=body.storage"
Hello Evgeniy.
Appreciate your feedback. I now understand I was using the 'payload' incorrectly (by default).
Although I did not fix this issue (getting error 500 now), at least I'm progressing. Will deal with this new issue.
Thank you.
Javier
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Maybe this will help you too. It's working function, which I'm using
ApplicationLinkService applicationLinkService = ComponentAccessor.getComponent(ApplicationLinkService)
ApplicationLink confLink = applicationLinkService.getApplicationLinks(ConfluenceApplicationType)?.first()
ApplicationLinkRequestFactory authenticatedRequestFactory = confLink.createAuthenticatedRequestFactory()
Map pageData = getWikiPageData(pageId) as Map
LinkedHashMap paramsBody = [
type : "page",
title : pageData['title'],
version: [
number: ((Integer) pageData['version']['number']) + 1
],
space : [
key: pageData['space']['key']
],
body : [
storage: [
value : pageBody,
representation: "storage"
],
],
]
String requestUrl = "rest/api/content/${pageId}"
authenticatedRequestFactory
.createRequest(Request.MethodType.PUT, requestUrl)
.addHeader('Content-Type', 'application/json')
.setRequestBody(new JsonBuilder(paramsBody).toPrettyString())
.execute(new ResponseHandler<Response>() {
@Override
void handle(Response response) throws ResponseException {
if (response.statusCode != HttpURLConnection.HTTP_OK) {
throw new Exception(response.getResponseBodyAsString())
}
}
})
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
For sure that it will help me.
I'm almost starting and all is very welcome to learn.
Appreciate it.
Regards
Javier
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
If this helps someone else ...
Once I fixed the initial problem (with the help of Evgeniy š) I encountered other issues. One of them was related to the certificates.
Use the openssl command to review the connection and, potentially, you will find a certificate issue. If you see errors, fix them and retry your confluence update:
openssl s_client -showcerts -connect xxx.xxx.xxx:443
Regards
Javier
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.