0

Here is simple request I try to do and watch response

def httpReq = new HTTPBuilder("http://${url}")
        httpReq.request(Method.GET, ContentType.JSON) {

            response.success = { resp ->
                println("Ok  " + resp.status.toString())
            }

            response.failure = { resp ->
                println("Not ok" + resp.status.toString())
            }

This returns an Error:

java.lang.ClassNotFoundException: org.apache.commons.collections.iterators.ArrayIterator

I think that ArrayIterator defined in jdk. I added manually org.apache.commons:commons-collections4:4.4 but it doesn't help.

cfrick
  • 35,203
  • 6
  • 56
  • 68
Duxa
  • 55
  • 1
  • 7

1 Answers1

2

TL;DR

Don't waste your time by gathering deps. Use a build tool like Gradle or Maven - or use Groovys Grapes if only a script is needed and Groovy is installed (or easily installable) on your target.

Using grape

You have to pick up all the transitive dependencies (this means the correct packages with the correct versions). E.g. with @Grab:

@Grab('org.codehaus.groovy.modules.http-builder:http-builder:0.7.1')
import groovyx.net.http.HTTPBuilder

def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )

If you really want to gather all the deps manually grape can also help. It can tell you, what jars are needed:

% grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.1
$HOME/.groovy/grapes/org.codehaus.groovy.modules.http-builder/http-builder/jars/http-builder-0.7.1.jar
$HOME/.groovy/grapes/org.apache.httpcomponents/httpclient/jars/httpclient-4.2.1.jar
$HOME/.groovy/grapes/org.apache.httpcomponents/httpcore/jars/httpcore-4.2.1.jar
$HOME/.groovy/grapes/commons-logging/commons-logging/jars/commons-logging-1.1.1.jar
$HOME/.groovy/grapes/commons-codec/commons-codec/jars/commons-codec-1.6.jar
$HOME/.groovy/grapes/net.sf.json-lib/json-lib/jars/json-lib-2.3-jdk15.jar
$HOME/.groovy/grapes/commons-beanutils/commons-beanutils/jars/commons-beanutils-1.8.0.jar
$HOME/.groovy/grapes/commons-collections/commons-collections/jars/commons-collections-3.2.1.jar
$HOME/.groovy/grapes/commons-lang/commons-lang/jars/commons-lang-2.4.jar
$HOME/.groovy/grapes/net.sf.ezmorph/ezmorph/jars/ezmorph-1.0.6.jar
$HOME/.groovy/grapes/net.sourceforge.nekohtml/nekohtml/jars/nekohtml-1.9.16.jar
$HOME/.groovy/grapes/xerces/xercesImpl/jars/xercesImpl-2.9.1.jar
$HOME/.groovy/grapes/xml-apis/xml-apis/jars/xml-apis-1.3.04.jar
$HOME/.groovy/grapes/xml-resolver/xml-resolver/jars/xml-resolver-1.2.jar

From this list you can build up your own classpath to pass to groovy and work without grape. E.g.:

% cat no-grape.groovy 
import groovyx.net.http.HTTPBuilder

def http = new HTTPBuilder('http://www.google.com')
def html = http.get( path : '/search', query : [q:'Groovy'] )

% groovy -cp `grape resolve org.codehaus.groovy.modules.http-builder http-builder 0.7.1 | paste -s -d:` no-grape.groovy
...
cfrick
  • 35,203
  • 6
  • 56
  • 68
  • Thanks. I tried to use Groovys Grape but it can't download dependencies with error Error grabbing Grapes [download failed: ...] in Intellij, also I installed groovy and tried to start script via windows cmd like this groovy -Dgroovy.grape.report.download=true MyScript.groovy and have Server access error at url repo1.maven.org/maven2/org/codehaus/groovy/modules/http-builder/… (java.net.ConnectException: Connection refused). But I can get data in browser. Also when I create maven project and add dependencies in pom everything is fine. Is it means that browser and maven are configured with proxy? – Duxa Dec 18 '19 at 03:49
  • Yes, most likely – cfrick Dec 18 '19 at 06:52