Why we receive an error trying to create a confluence page from Jira cloud (scriptrunner)

shira segal inbar May 23, 2018

Hi All,

I managed to produce a confluence page from Jira cloud with Scriptrunner .

The problem is that it does not always work. I've figured out that multi-text fields (description) have a problem when they have a line break.

My question to Scriptrunner Support

With help from Scriptrunner I managed to remove line breaks (JsonStringEncoder) but the Script still not working.

Now the description text is without breaks but with \r\n (replaceAll didn't work)

The code:

import com.fasterxml.jackson.core.io.JsonStringEncoder

def baseUrl = new URL("https://myurl.atlassian.net/wiki/rest/api/content")
def issueKey = issue.key

def result = get('/rest/api/2/issue/' + issueKey)
.header('Content-Type', 'application/json')
.asObject(Map)

String Description = result.body.fields["description"]
def USER_NAME_AND_PASS = "MyUser:pass"

JsonStringEncoder e = JsonStringEncoder.getInstance();
String encodedDescription = new String(e.quoteAsString(Description));


def body = '''
{
"type" : "page",
"status" : "current",
"title" : "NGS Kickoff Meeting Protocol (Project Number: '''+IssueKey+''') ['''+date+''']",
"space" : { "key" : "INCPM" },
"ancestors": [{
"id": 318963721
}],
"body" : {
"storage" : {
"value": "'''+encodedDescription+'''",
"representation" : "storage"
}
}
}'''

def connection = baseUrl.openConnection()
def authString = USER_NAME_AND_PASS.getBytes().encodeBase64().toString()
connection.setRequestProperty( "Authorization", "Basic ${authString}" )
connection.setRequestProperty( "Content-type", "application/json");
connection.setRequestProperty( "Accept", "application/json" );
connection.with {
doOutput = true
requestMethod = 'POST'
outputStream.withWriter { writer ->
writer << body

}
println content.text
}

Thanks,

Shira

1 answer

1 accepted

0 votes
Answer accepted
Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 23, 2018

Hi Shira,

Can you please post the contents of the description variable so we can see why the JSON encoder is not working for this variable with line breaks.

Regards,

Kristian

shira segal inbar May 24, 2018

Hi Kristin,

The content of the description before the JSON encoder:

Created by Shira
hhh*&

test this

The content with the  JSON encoder:

Created by Shira\r\nhhh*&\r\n\r\ntest this \r\n

 

Thanks,

Shira 

shira segal inbar May 24, 2018

Hi, 

I managed to remove the \r\n char

String jsonFormattedString = encodedDescription.replaceAll("\\\\r", " ").replaceAll("\\\\n", "")

and now my content is :

Created by Shira hhh*&  test this  

Receive this error:

java.io.IOException: Server returned HTTP response code: 400 for URL: at Script1$_run_closure1.doCall(Script1.groovy:61) at Script1.run(Script1.groovy:55) at Script1$run.call(Unknown Source) at Script1$run.call(Unknown Source) at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:32) at com.adaptavist.sr.cloud.workflow.AbstractScript$evaluate$2.callCurrent(Unknown Source) at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:27) at ConsoleScriptExecution1_groovyProxy.run(Unknown Source)

And the page was not created...

You have an idea for a solution?

 

Thanks,

Shira

Jon Bevan [Adaptavist]
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
May 25, 2018

Hi Shira,

What is the response body from that POST request to /wiki/rest/api/content?

Normally with a 400 Bad Request response you'd get a response body containing information about what was wrong with the request.

Thanks,
Jon

shira segal inbar May 30, 2018

Hi, 

The problem was the character &.

Now everything works great.

 

Thanks,

Shira

Kristian Walker _Adaptavist_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
May 30, 2018

Hi Shira,

Thank you for confirming that this issue has been resolved.

Kristian

Suggest an answer

Log in or Sign up to answer