Wednesday, 13 August 2014

Executing Shell commands in Groovy


Groovy is a dynamic java language that runs on JVM. If you are familiar with Java that you will find it very easy to understand and code in Groovy. Will not go into much details now. You can refer to my earlier posts - 

Goal of this post is to show how easy it is to execute shell commands from Groovy. I am using Windows for demonstrating this but should be irrespective of OS you are using. All you have to do is make sure cURL is installed on your system which I will be using to demonstrate this example. You can refer to following posts on cURL

To the code....

 Put the following code in a file name GroovyTest.groovy and run it

class GroovyTest {
    public static void main(def args){
        def url = '';
        println("Output : " + executeCurlCommand(url));
    def static executeCurlCommand(URL){
        def url = "curl " + URL;
        def proc = url.execute();
        def outputStream = new StringBuffer();
        proc.waitForProcessOutput(outputStream, System.err)
        return outputStream.toString();

After running you can see the output same as executing the command on your console. If you need all outputs on standard output you can do

proc.waitForProcessOutput(System.out, System.err);

You can see the output by executing the same command in console

Using HTTP POST and GET using cURL


cURL is a command line utility to perform GET and POST requests. It comes really handy for testing REST APIs. For debian based Linux systems you can simply install it

  • sudo apt-get install curl

But you can install  it in windows as well. You can use it from cygwin as well (select the appropriate package while installing). For installing cURL in windows I had written a post some time back. You can refer the same -

That's about installation. In this post we will see the usage if cURL command.

Using cURL command

As we know there are two majorly used REST APIs - GET and POST. Lets see how to perform these actions using cURL - 


  1. With json :

    curl -i -H "Accept: application/json" -H "Content-Type: application/json" -X GET http://hostname/resource

  2. With XML :

    curl -H "Accept: application/xml" -H "Content-Type: application/xml" -X GET http://hostname/resource


  1. For posting data :

    curl --data "param1=value1&param2=value2" http://hostname/resource

  2. For File Upload :

    curl --form "fileupload=@filename.txt" http://hostname/resource

  3. RESTful HTTP post :

    curl -X POST -d @filename http://hostname/resource

  4. For logging into a site (auth):

    curl -d "username=admin&password=admin&submit=Login" --dump-header headers http://localhost/Login
    curl -L -b headers http://localhost/

POST with some JSON data

You can use -H to set the header while using cURL command 

Example  - 
  • -H "Content-Type: application/json"
So your complete command would look something like -

curl -H "Content-Type: application/json" -d '{"username":"xyz","password":"xyz"}' http://localhost:3000/api/login

Difference between GET and POST

Related Links

t> UA-39527780-1 back to top