Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy Script to POST nested data to external REST API

terence_harvey
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 7, 2020

Hi

I am attempting to POST data from a Groovy Script to an external REST API. The external API requires the data to be in the format:

http://someSite/someAPI/SomeAction?AuthToken=S_AUTH_TOKEN&domainName=aS_DOMAIN&PRODUCT_NAME=S_PRODUCT_NAME&input Data=[{"RequestAction":"S_RA", "Subject":"A Subject","accounts":[{"name":"John Doe","firstName":"John","lastName":"Doe" },{"name":"Jane Doe","firstName":"Jane","lastName":"Doe" }]}]

In order to achieve this, I am using an HTTPBuilder:

def baseURL = "http://someSite/someAPI";
def baseAction = "/SomeAction";

def httpBuilderRequest = new HTTPBuilder(baseURL + baseAction);
httpBuilderRequest.request(POST) {
requestContentType = ContentType.JSON
body = [AuthToken: "S_AUTH_TOKEN"
, PRODUCT_NAME: "S_PRODUCT_NAME"
, domainName: "S_DOMAIN"
, inputData:
[{RequestAction:"S_RA", "Subject":"Some Subject", accounts: [{"name":"John Doe","firstName":"John","lastName":"Doe" }]}]
]
response.success = {resp, JSON ->

}
response.failure = {resp, JSON ->
}
}

 The "accounts" data is derived from a Jira issue obtained earlier, but that is not the issue. I have tried:

  • Sending a string URL with the data as pre-formatted query string without a body (this works successfully in PostMan). This attempt returns an Illegal character in query error pointing to the first instance of a square bracket
  • Building the body as above. This does not work as Groovy is not happy with the formatting of the nested objects (See images below)
  • Using a JsonSlurper to parse the data. This simply replaces all the braces {} with square brackets and the external API cannot read valid Json

 

Groovy001.JPG Groovy002.JPG

Is there perhaps anyone who could assist with a solution to this problem (Jira Server 8.19.9 is being used)

 

Thank you in advance

0 answers

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
TAGS
atlassian, confluence, loom, atlassian intelligence, ai notes, ai-powered meeting notes, atlassian community events, ace, confluence ai, loom ai integration, ai note-taking, atlassian ai features, team '25, atlassian live learning, confluence automation

Unlock AI-powered meeting notes: Join our live learning session! 📹

Did you catch the news at Team ‘25? With Loom, Confluence, Atlassian Intelligence, & even Jira 👀, you won’t have to worry about taking meeting notes again… unless you want to. Join us to explore the beta & discover a new way to boost meeting productivity.

Register today!
AUG Leaders

Atlassian Community Events