[groovy-user] Groovy : REST Example ( HTTP Builder )

2,286 views
Skip to first unread message

Parth Vora

unread,
Jun 6, 2013, 6:11:41 PM6/6/13
to us...@groovy.codehaus.org

Hello All,

 

I am trying to use a HTTP builder to automate a simple rest call.

 

I keep getting MissingMethodException.

 

My REST Query is as follows :

 

URL : someurl

Headers : h1:value1, h2:value2

Method : GET

 

I don’t know what to import. Can someone help ?

 

For now I am using :

import static groovyx.net.http.Method.GET

import static groovyx.net.http.ContentType.TEXT

import groovyx.net.http.HTTPBuilder

 

Am  I missing something ?

 

-Thanks,

Parth.

Andrey Bloschetsov

unread,
Jun 7, 2013, 2:18:39 AM6/7/13
to us...@groovy.codehaus.org
Hello Parth,

Could you show your example code?

Here is an example using HttpBuilder:
http://groovy.codehaus.org/HTTP+Builder

2013/6/7 Parth Vora <Parth...@citrix.com>:
--
Andrey Bloschetsov

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

http://xircles.codehaus.org/manage_email


Parth Vora

unread,
Jun 10, 2013, 8:47:33 PM6/10/13
to us...@groovy.codehaus.org
Hello,

Here you go..

import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.TEXT
import groovyx.net.http.HTTPBuilder

def http = new HTTPBuilder('http://URL')
ERROR is caught here --> http.request( Method.GET, ContentType.TEXT ) { req ->
uri.path = '/some-path'
headers.Accept = 'application/json'
headers.ClientRoles = 'ROLES'
headers.Authorization = 'Auth code'


response.success = { resp, reader ->
println "Got response: ${resp.statusLine}"
println "Content-Type: ${resp.headers.'Content-Type'}"
print reader.text
}
}

ERROR :

Caught: groovy.lang.MissingPropertyException: No such property: Method for class: RestCall
groovy.lang.MissingPropertyException: No such property: Method for class: RestCall
at RestCall.run(RestCall.groovy:27)

------------------------------------------------------------------------------------------------------------------------------------

LINE 43 : new HTTPBuilder('http://www.google.com/').request(GET) { req ->

response.success = { resp ->
println 'request was successful'
assert resp.status < 400
}

response.failure = { resp ->
println 'request failed'
assert resp.status >= 400
}
}

Caught: groovy.lang.MissingMethodException: No signature of method: groovyx.net.http.HTTPBuilder.request() is applicable for argument types: (groovyx.net.http.Method, RestCall$_run_closure1) values: [GET, RestCall$_run_closure1@38bdc9b3]
Possible solutions: request(groovyx.net.http.Method, groovy.lang.Closure), request(groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure), request(java.lang.Object, groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: groovyx.net.http.HTTPBuilder.request() is applicable for argument types: (groovyx.net.http.Method, RestCall$_run_closure1) values: [GET, RestCall$_run_closure1@38bdc9b3]
Possible solutions: request(groovyx.net.http.Method, groovy.lang.Closure), request(groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure), request(java.lang.Object, groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure)
at RestCall.run(RestCall.groovy:43)

-Thanks,
Parth.

---------------------------------------------------------------------------------------------------------------------------------------

Bob Brown

unread,
Jun 11, 2013, 1:11:25 AM6/11/13
to us...@groovy.codehaus.org
This is due to incorrect use/understanding of "import static..."

Once you have done:

import static groovyx.net.http.Method.GET

You only need to use:

GET

NOT:

Method.GET

See:

http://docs.oracle.com/javase/1.5.0/docs/guide/language/static-import.html

HTH

BOB

Parth Vora

unread,
Jun 11, 2013, 8:29:07 PM6/11/13
to us...@groovy.codehaus.org
Thanks Bob. I got that.

I tried this but all in vain I still get the same error.

import groovyx.net.http.HTTPBuilder
importstatic groovyx.net.http.Method.GET
importstatic groovyx.net.http.ContentType.JSON

def http = new HTTPBuilder( 'URL' )
http.request( GET, JSON ) {
uri.path = '/somepathURL'
headers.Accept = 'application/json'
headers.ClientRoles = 'ROLE'
headers.Authorization = 'AuthToken'

response.success = { resp, json ->
println "Got response: ${resp.statusLine}"
println "Content-Type: ${resp.headers.'Content-Type'}"
print reader.text
}
}

Caught: groovy.lang.MissingMethodException: No signature of method: groovyx.net.http.HTTPBuilder.request() is applicable for argument types: (groovyx.net.http.Method, groovyx.net.http.ContentType, RestCall$_run_closure1) values: [GET, application/json, RestCall$_run_closure1@c96ad7c]
Possible solutions: request(groovyx.net.http.Method, groovy.lang.Closure), request(groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure), request(java.lang.Object, groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure)
groovy.lang.MissingMethodException: No signature of method: groovyx.net.http.HTTPBuilder.request() is applicable for argument types: (groovyx.net.http.Method, groovyx.net.http.ContentType, RestCall$_run_closure1) values: [GET, application/json, RestCall$_run_closure1@c96ad7c]
Possible solutions: request(groovyx.net.http.Method, groovy.lang.Closure), request(groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure), request(java.lang.Object, groovyx.net.http.Method, java.lang.Object, groovy.lang.Closure)
at RestCall.run(RestCall.groovy:6)

Bob Brown

unread,
Jun 12, 2013, 6:28:33 AM6/12/13
to us...@groovy.codehaus.org
This works for me with Groovy 2.1.4:

---
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.6' )

import groovyx.net.http.HTTPBuilder
import static groovyx.net.http.Method.GET
import static groovyx.net.http.ContentType.JSON

new HTTPBuilder().request('http://baseballipsum.apphb.com/api/?paras=1&startwithlorem=true', GET, JSON ) {
// headers don't do anything useful in this example...
headers.Accept = 'application/json'
headers.ClientRoles = 'ROLE'
headers.Authorization = 'AuthToken'
//

response.success = { resp, reader ->
println "Got response: ${resp.statusLine}"
println "Content-Type: ${resp.headers.'Content-Type'}"
println reader
}
}
---

Note that the use of 'reader' is slightly different from what is given in the doco at: http://groovy.codehaus.org/modules/http-builder/doc/get.html
Note also that there are easier ways of doing a GET than using the response() method.

HTH

Bob

> -----Original Message-----
> From: Parth Vora [mailto:Parth...@citrix.com]
> Sent: Wednesday, 12 June 2013 10:29 AM
> To: us...@groovy.codehaus.org
> Subject: RE: [groovy-user] Groovy : REST Example ( HTTP Builder )
>

Parth Vora

unread,
Jun 13, 2013, 3:40:40 PM6/13/13
to us...@groovy.codehaus.org
Hello Bob,

Can you send me the snap shot of your pom file where you have mentioned your groovy version ?

I can replicate that and try re-running it !

-Thanks,
Parth.

Bob Brown

unread,
Jun 13, 2013, 5:11:55 PM6/13/13
to us...@groovy.codehaus.org
The script will run as-is. Just copy it into the groovy console and run it.

See:

http://groovy.codehaus.org/Grape

Bob

Sent from my iPad
Reply all
Reply to author
Forward
0 new messages