Hello,
I would like to ask for your help regarding REST Endpoint in ScriptRunner.
The payload from the API call looks like this:
"content":
"{
\"affectedVersion\":[],
\"billing\":[\"Test\"],
\"browser\":[\"Browser default value\"],
\"environment\":[\"dev\"],
\"exceptions\":[\"Default exceptions value\"],
\"attachments\":[
{
\"filename\":\"2019-08-14 21_48_05-Window.jpg\",
\"contentType\":\"image/jpeg\",
\"content\":\"\noAKKKKACi...KKKACiiigD\"
}]
}
I am able to retrieve everything through the REST Endpoint script except the attachments. I would like to retrieve the filename, contentType, and content and put it into variables as for all other parameters (affectedVersion, billing, browser...).
However, the code provided by the support (https://bitbucket.org/snippets/Adaptavist/jeGxa8) throws an error:
def forms = new JsonSlurper().parseText(body) as Map<String,String>
forms.attachments.each { String fileName = it.filename // not working String contentType = it.contentType // not working String content = it.content // not working
}
So, I am unable to pass the data from the payload to the variables and create the attachments.
Is there any other way to retrieve these 'nested' attributes from the API call?
I was looking at http://grails.asia/groovy-each-examples, but there is no example of the case I am dealing with.
I would be very happy for any help!
Thanks!
"not working" is a little vague.
At first glance, your code is what I would use to access those nested object.s But since we have no way of being sure that this is how the REST API is actually retrieving it, you may need to add a whole bunch of debug message to get to the cause.
def forms = new JsonSlurper().parseText(body) as Map<String,String>
log.debug "forms contains: ${forms.keySet()}"
log.debug "attachments is of class ${forms.attachments.getClass()}" //expect arrary
log.debug "attachments contains ${forms.attachments.size()} items"
forms.attachments.each { log.debug "each attachment is of class ${it.getClass()}" //expect map
log.debud "attachment (it) has keys: ${it.keySet()}"
String fileName = it.filename // not working String contentType = it.contentType // not working String content = it.content // not working
}
Examining your log output with these should give you a clue. Either "attachments" is not an array and each item is not a map. Or something else is wrong and you'll get some stack trace.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.