12

I am very newbie for this groovy programming. I have written an API which is running in my local system (localhost:2100).

Now i want to make a simple GET request to this API using groovy code. I searched a lot for this but i cant find the clear guidance.

I tried for this :

http://www.kellyrob99.com/blog/2013/02/10/groovy-and-http/ http://rest.elkstein.org/2008/02/using-rest-in-groovy.html etc.. But nothing works.

Also i came across HttpBuilder. I cant get clear idea of this. Please share your ideas.

EDIT:

I tried for this:

def client = new RESTClient("http://localhost:2100");
def res = client.get(path:"xxx/yyy/zzz")

I am receiving error:

Groovy:unable to resolve class RESTClient

Whether I need to add dependency in my pom.xml?

Opal
  • 81,889
  • 28
  • 189
  • 210
Subburaj
  • 5,114
  • 10
  • 44
  • 87

3 Answers3

21

If you need to do a simple GET petition. You can use the URL Class. For example, to get the content of example.org, with a GET request

new URL("http://example.org/").text
Fernando
  • 2,131
  • 3
  • 27
  • 46
1
import groovyx.net.http.HTTPBuilder;

public class HttpclassgetrRoles {
     static void main(String[] args){

         def baseUrl = new URL('http://test.xyz.com/api/state/GetUser')
         HttpURLConnection connection = (HttpURLConnection) baseUrl.openConnection();
         connection.addRequestProperty("Accept", "application/json")
         connection.with {
           doOutput = true
           requestMethod = 'GET'
           println content.text
         }

     }
}

It is definitely worked for me

SQA
  • 39
  • 11
0

RESTClient class is not a part of a standard groovy library. You not only need an appropriate maven entry but you need to import (via import statement in the script) appropriate classes you need to use.

Here's a wiki site for HTTPBuilder.

Opal
  • 81,889
  • 28
  • 189
  • 210
  • Actually i added that repository in maven POM.xml . SO the above problem is solved. but HTTP request is coming into my API layer. – Subburaj Oct 20 '15 at 05:44
  • @Subburaj, if the problem is solved and my answer was helpful please accept and upvote it. – Opal Oct 20 '15 at 05:49