Friday, July 13, 2018

cURL command line tutorials with examples

cURL command basics

This tutorial shows the basics of cURL command line tools, cURL Installation on Linux/Unix and windows.

CURL is a command line tool used to send a data between two machines. data can be transferred via HTTP, FTP protocols of any URL/file. cURL command is used to test API during the development phase. This tool is available in Windows, Linux, Unix and Mac operating systems.

Understanding cURL command Usage

  • Retrieving Content from URL
  • Testing REST API
  • Upload/download files
  • get Response and Location headers of any remote URL or websites

cURL command install and configure on  windows

For developers, This is the basic and important command to test REST API's in a console

 Please download CURL executable cURL executable to install/configure on windows and extract the downloaded zip folder to the filesystem as below

Install Curl command line tool on windows

If you want to access curl command globally, Please setup environment variable by adding curl.exe to PATH environment variable.

CURL Command line tools setup in Linux/Unix

we can setup curl command tools easily using apt-get command. Root permission required to install curl command tools

sudo apt install curl

Basic Usage of cURL syntax

this command has minimum tow parameters one is to use the double hyphen as part of options and other is URL or a file

Usage: curl [options...] {url}

Various options of cURL tools

We will go through some basic options of the command line

Options Description
-U Proxy URL user details login and password
-A send request headers parameters as part of a request to the server
-m command full documentation about this command
-O Multiple files/URLs support
-L Location information
-V retrieve full request and response header information of a URL
-I retrieve full response header information
-x proxy support
-T Upload a file support
-X Request method type support
-H Modify request header
-d request body data
The below section will see curl examples with tutorials and CURL command line tools options

Retrieve the content of URL/file

This is the basic command to retrieve the cloudhadoop site content. By default, the content will be out to console

curl cloudhadoop.com

Multiple Files/URLS download content

To retrieve multiple file content, use -o option

curl -O file1 file2 URL1

Print URL response headers using Curl command

Use option -I to retrive the response headers,

C:\curl\src>curl -I http://www.cloudhadoop.com
HTTP/1.1 200 OK
Content-Type: text/html; charset=UTF-8
Expires: Fri, 13 Jul 2018 09:33:14 GMT
Date: Fri, 13 Jul 2018 09:33:14 GMT
Cache-Control: private, max-age=0
Last-Modified: Fri, 13 Jul 2018 07:23:27 GMT
ETag: "d2546e8a-0752-47f1-b30c-0e4a5535b852"
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
Content-Length: 0
Server: GSE

How to get request response headers with content using CURL

cURL command with -v is used to retrieve verbose of any url. Please see below screenshot
cURL command Verbose windows

cURL command usage with HTTP methods GET/POST/PUT/Delete 

curl without any options to request URL is treated as GET request. to send different requests method type, use -X option. This is command line version of postman client for Testing web service API.



curl -H "Content-Type: application/json"  --d '{"id":"1"}'   http://localhost:3000/get/customer
curl -H "Accept: application/json" -H "Content-type: application/json" -X POST -d '{"id":1}' http://localhost/api/customers



This is my understanding on curl command tools in Linux/windows. Please share your comments if you have any questions


EmoticonEmoticon

Note: Only a member of this blog may post a comment.