Category Archives: bash

What is cURL?

cURL is a tool for working with URLs. cURL help us to query a URL from the command line. It lets us post form data, FTP to a server, and much, much more. One of the places that cURL comes in handy most often is while working with APIs. With cURL, we can try out new APIs in a simple manner, with just the command line, with no need for installing, or writing complex wrappers around the API. The simplest thing we can do with curl is to make an HTTP request to given server and print its response out to the server.

1.Installation

Installation On Windows Environment

  1. Download curl zip
  2. Extract the contents, if you are downloaded current file you will find ‘curl.exe’
  3. Place curl.exe in a folder where you keep your software (e.g. E:\software\curl\curl.exe)
  4. To run curl from the command line
    • Go to tab system environment variables settings
    • Under System variable select ‘PATh’ and Edit button
    • Add a semicolon followed by the path to where you placed your curl.exe (e.g. ;E:\software\curl)

If you install Git Bash for windows, you get cURL automatically.Git will take care of ‘PATH’ setup during installation automatically

On Linux and Mac environment, you already have cURL installed on your machine

2.Commands

2.1 Basic Commands

The simplest thing we can do with curl is to make an HTTP request to given server and print its response out to the server.
eg:

> curl http://www.google.com

You can see the response will be print on terminal.

2.2 Request Methods

cURL supports all of the HTTP request methods like GET, POST, PUT, DELETE.
We can use ‘-X’ file to change the request methods.
Let us make a simple GET request.

> curl  -X  GET  'http://localhost:8080/api/?action=close&country=uk&accessToken=0f6e4a16b7ff282c57cdbaa1023'

The above statement will print response on the terminal/console

> {"error":"Invalid Token value"}'

Send a POST request.You don’t need to do extra work, just replace GET with POST.

> curl  -X  GET  'http://localhost:8080/api/?action=close&country=uk&accessToken=0f6e4a16b7ff282c57cdbaa1023'

The above statement will print response on the terminal/console

> {"error":"Invalid Token value"}'

2.3 Passing data in request body(-d) with GET and POST methods.

So far we have sent parameters to the server on query string parameters. Usually, the GET method displays all the parameters in the URL field of our browsers. That’s generally a good thing when you want to be able to bookmark that page with your given data, but it is a disadvantage if you entered secret information in one of the fields or if there are a large amount of fields creating a very long and unreadable URL.Instead of passing data via query strings let sent in the request body.

2.3.1 Passing data in GET method

cURL provides the -d flag to us pass data as part of the request body.

>curl  -X POST  'http://localhost:8080/api  \
 -d action=close \
 -d country=uk \
 -d accessToken=0f6e4a16b7ff282c57cdbaa1023gh4

If you repeat -d several times on the command line, curl will concatenate all the given data pieces – and put a ‘&’ symbol between each data segment.

2.3.2 Passing data in GET method

cURL provides a -G flag.

This option will make all data specified with -d, –data, –data-binary or –data-URL encode to be used in an HTTP GET request instead of the POST request that otherwise would be used. The data will be appended to the URL with a ‘?’ separator.

>curl  -X GET -G  'http://localhost:8080/api  \
 -d action=close \
 -d country=uk \
 -d accessToken=0f6e4a16b7ff282c57cdbaa1023gh4

The data you send to the server should already be properly encoded, curl will not do that for you. For example, if you want the data to contain a space or + etc, you need to replace that space with encode values. Failing to comply with this will most likely cause your data to be received wrong and messed up.

cURL provides a –data-urlencode flag.
This posts data, similar to the other –data options with the exception that this performs URL-encoding.

> curl  -X GET -G  'http://localhost:8080/api  \
 -d action=close \
 -d country=uk \
 -d accessToken=0f6e4a16b7ff282c57cdbaa1023gh4 \
--data-urlencode email=abc+k@gmial.com 

2.4 Show Response Headers

If we want to see more information about the response, we can make the request using the ‘-i’ flag. This will include the HTTP-header in the output. The HTTP-header includes things like server-name, date of the document, HTTP-version and more.

Example:

>curl  -X GET -G  'http://localhost:8080/api  \
 -d action=close \
 -d country=uk \
 -d accessToken=0f6e4a16b7ff282c57cdbaa1023gh4 \
--data-urlencode email=abc+k@gmial.com 

You could see response headers in the response.

> HTTP/1.1 400 Bad Request
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=FFC8F036A9044B9C40411B97CBCDFD3D; Path=/; HttpOnly
Content-Type: application/json;charset=UTF-8
Transfer-Encoding: chunked
Date: Sat, 10 Oct 2015 10:03:35 GMT
Connection: close

{"error":"Invalid member id"}

2.5 Setting Headers

cUrl provides -H flag to set the HTTP headers. Extra header to include in the request when sending HTTP to a server.

Here is an example which sent a header in the request.Here am passing authentication header.

>curl -X  GET -G -i -H 'Authorization:bearer 0f6e4a16b7ff282c57cdbaa1023gh4' 
 'http://localhost:8080/api  \
 -d action=close \
 -d country=uk \
 -d accessToken=0f6e4a16b7ff282c57cdbaa1023gh4 \
--data-urlencode email=abc+k@gmial.com 

2.6 HTTP Authentication

HTTP Authentication is the ability to tell the server your username and password so that it can verify that you’re allowed to do the request you’re doing.
cURL provides -u flag, specify the user name and password to use for server authentication.

>curl -u abc:abc -X GET  -G -i  'http://localhost:8080/api \
 -d action=close \
 -d country=uk \
 -d accessToken=0f6e4a16b7ff282c57cdbaa1023gh4 \
--data-urlencode email=abc+k@gmial.com 

recode utf8:html < monitor.xml