Showing posts with label CURL. Show all posts
Showing posts with label CURL. Show all posts

Wednesday, 13 August 2014

Executing Shell commands in Groovy

Background

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 = 'http://mail.google.com';
        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

Background

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 - 

GET

  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

POST


  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


Saturday, 10 May 2014

Download and Install cURL on Windows

Introduction to cURL

As per Wiki

cURL is a computer software project providing a library and command-line tool for transferring data using various protocols. The cURL project produces two products, libcurl and cURL. It was first released in 1997.

In this post we will see how do we install this tool and use it to perform some common HTTP requests like GET.

Installing cURL on Windows

  1. Go to curl Download Wizard
  2.  Select curl executable
  3. Select Win32 or Win64
  4. Then select package for it(Eg generic/cygwin) as per your requirement
  5. Then you will have to select version. You can select unspecified.
  6. This will directly take you to download link which on click will give you popup to download the zip file.
  7. Extract the zip to get the executable.(I have put the executable in C:\Curl
  8. Add this folder in your environment variables and you are done.

  9. You can then execute curl command from cmd.








PS : I had some problems with proxy. Though I don't have one cURL seems to pick it from somewhere. I was using that proxy about a year ago and have not used since. Not sure where cURL picks this confog from. But as a workaround I have use

curl --noproxy * websiteURL


This will essentially disable proxy. But you may not have to do so.

For more reading on various practical usages of curl command you may refer to

15 Practical Linux cURL Command Examples

Important Links

t> UA-39527780-1 back to top