Hi folks,
I'm trying to post to a rest api using scriptrunner:
import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.ContentType.*
import static groovyx.net.http.Method.*
import org.apache.log4j.Logger
import org.apache.log4j.Level
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)
def http = new HTTPBuilder('https://serviceportal.pornhub.local:443/')
def body = '{"parameters": [ { "value": { "string": { "value": "testu" } }, "type": "string", "name": "username", "scope": "local" }]}'
try
{
http.request( POST, JSON ) {req ->
uri.path = 'vco/api/workflows/34c1dc35-1d56-40ec-8741-18fb6ed0d59e/executions/'
body = body
response.success = { resp ->
println "POST response status: ${resp.statusLine}"
assert resp.statusLine.statusCode == 201
}
}
}
catch (groovyx.net.http.HttpResponseException ex) {
log.debug ex.printStackTrace()
log.debug ex.toString()
}
The call works fine in postman but all i get when i run this through the script console is a groovyx.net.http.HttpResponseException with no details as to what I am doing wrong. Can anyone point out what I am doing wrong?
thanks
I do a similar thing, but where it makes sense, I also store the template in an Insight object so that it's easy to update.
A couple of things I might suggest
1) Add a try/catch block because if you have a ${variable} in your template that you don't supply in the binding, the script will just crash. You might want to have some fall-back options.
2) Don't use .vm extension. I think that implies a "velocity template" which is a different kind of template with a different language and syntax.
I use the SimpleTemplateEngine instead of the GStringTemplate engine. I think for your specific use case, it would also work. According to the documentation the GStringTemplateEngine "stores the template as writeable closures (useful for streaming scenarios)", I'm not sure what that means in practical terms.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.