Web Request: GET Get Page into PUT Update Page is not working with regards to storage value

Anthony Nguyen
Contributor
June 17, 2024

I am working with Automation and web request action. The first step is below:

GET https://{site}/wiki/api/v2/pages?spaceid={spaceid}&title={title}&body-format=storage

I then store {{webResponse.body.results.body.storage.value}} in variable {{content}} and log it.

Next, I use below:

PUT https://{site}/wiki/api/v2/pages/{pageid}

{ 

"id": "{{webResponse.body.results.id}}",

"status": "current",

"title": "{{webResponse.body.results.title}}",

"body": { "representation": "storage", "value": "{{content}}" },

"version": { "number": {{#increment}}{{webResponse.body.results.version.number}}{{/}}, "message": "" }

}

There appears to be a problem with me passing back in {{content}}. Unfortunately, while this works if the page is super simple (just text), anything more complicated like elements or pictures causes the below:

 

Error publishing web request. Response HTTP status:
400
Error response HTTP body:
{"errors":[{"status":400,"code":"INVALID_MESSAGE","title":"Invalid message","detail":null}]}

The main idea is that I can change the title and append more information to the page, i.e. {{content}} {{issue.summary}}. However, I cannot get this first part to function correctly. I am basing this process on the details provided here:

https://community.atlassian.com/t5/Automation-articles/Jira-Automation-Updating-a-Confluence-page-with-information-from/ba-p/2660113

 


UPDATE: I believe I found one of the problems! If the {{content}} contains any " without escaping them first, it just won't work. So to fix, I need to figure out a way to do a regex replacement. 


UPDATE2: Use replace("\"", "\\\"") to fix the issue. Everything's working now, I could almost cry!


UPDATE3: I believe the proper way to address the problem is to use .jsonEncode so it will properly handle any chars that need escaping.

2 answers

1 accepted

0 votes
Answer accepted
Anthony Nguyen
Contributor
June 17, 2024

If {{content}} contains any " without escaping them first, it won't work. Use replace("\"", "\\\"") to fix the issue.

 

I believe the proper way to do this is to actually utilize .jsonEncode instead of relying on replacements.

0 votes
Shawn Doyle - ReleaseTEAM
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 17, 2024

body-format=storage is XML.  I would guess this isn't formatted correctly, and it is expecting a string and not XML.

"body": { "representation": "storage", "value": 

If I have time later today I will attempt to reproduce what you are attempting, and see if I can help.

Anthony Nguyen
Contributor
June 17, 2024

Thank you! I was pulling my hair out because this process seemed to work for everyone else. That's the thing though, even in the linked example, they passed it right back in without issue.

Anthony Nguyen
Contributor
June 17, 2024

I've updated my question, but it has to do with unescaped " in {{content}}. I need to set up regex replacement so that " are instead \".

Shawn Doyle - ReleaseTEAM
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 17, 2024

There are several regex generators on line, google and bookmark your favorite.

Or ask your favorite AI tool.

 

Anthony Nguyen
Contributor
June 17, 2024

Is there anyway to do it in Automation? I tried {{content.replace(""","\"")}} but that didn't work.

Shawn Doyle - ReleaseTEAM
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
June 17, 2024
Anthony Nguyen
Contributor
June 17, 2024

Ok, it's replace("\"", "\\\"") for posterity's sake.

Like # people like this
Sylvain Leduc
Contributor
December 6, 2024

Thank you @Anthony Nguyen !

Let me add the automation I had to do :

Trigger : anything, a ticket updated for example

Then :

1/ send web request GET to get the content + version

<conf url>/rest/api/content/<confluence page id>?expand=version,body.storage

2/ Log action : Webhook response: {{webhookResponse.body}}

3/ create variable name = newVersion and smart value = {{webhookResponse.body.version.number.plus(1)}}

4/ log action : Calculated version: {{newVersion}}

5/ create variable name = existingContent and smart value = {{webhookResponse.body.body.storage.value}}

6/ log action Calculated version: {{existingContent}} (not sure this one is required ?)

7/ finally send the web request PUT to put this custom data :

{
"id": "<confluence page id>",
"type": "page",
"title": "your title",
"space": {
"key": "SPC"
},
"version": {
"number": {{newVersion}}
},
"body": {
"storage": {
"value": "<html><body>{{existingContent.replace("\"", "\\\"")}}<p>New ticket: {{issue.url}}, labels: {{issue.labels}}</p></body></html>",
"representation": "storage"
}
}
}

Working like a charm !

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events