cancel
Showing results for 
Search instead for 
Did you mean: 

Calling RESTClient from Script Task

mike_waldrop
Champ in-the-making
Champ in-the-making
I'm trying to find the most simple way to make outbound REST calls from a script task.  I was hoping to keep it all simple with groovy with something like:


import groovyx.net.http.RESTClient
import static groovyx.net.http.ContentType.*

def client = new RESTClient( 'https://hooks.slack.com/services/' )
def resp = client.post( path : 'testpath',
                        body : [channel: "test", username: "test content"],
                        requestContentType : JSON
                       )
assert resp.status == 200  // HTTP response code; 404 means not found, etc.
println resp.getData()

So, the problem I have initially is just to get the correct http-builder jar in the class path that will get picked up.  I don't have a completely mavenized development environment, and I'm trying to keep it simple.

I tried dynamically loading the dependencies via grape with something like:

@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7' )
import static groovyx.net.http.ContentType.*

def client = new groovyx.net.http.RESTClient( 'https://hooks.slack.com/services/' )
def resp = client.post( path : 'test path',
                        body : [channel: '#stest',
                                username: 'mike',
                                text: 'test message'],
requestContentType : JSON
                       )
assert resp.status == 200  // HTTP response code; 404 means not found, etc.
println resp.getData()

But I still get some class not found errors.  I can generally get these scripts working fine from a normal groovy command line in the OS.  Any pointers on how to keep it simple ?
1 REPLY 1

jbarrez
Star Contributor
Star Contributor
Hi Mike, nice seeing you here 😉

A quick googling on the @Grab gave me this : http://stackoverflow.com/questions/20009993/groovy-how-does-grab-inclusion-differ-from-classpath-inc...

"Grab uses ivy to fetch the specified library (plus all of its dependencies) from the maven core repository. It then adds these downloaded libraries to the classpath of the loader that's running tge current script."

So for Grab to work, it seems it needs Ivy on the classpath. Which probably is included by default in the Groovy binary when running from commandline. So adding the Ivy jars might do the trick.

Since you are using Groovy, you can also - instead of using the http builder - use the standard JDK classes. Or even HttpClient.