getting started with curl


cURL command is used when two servers want to communicate with each other by following protocols such as  FTP, GOPHER, HTTP, HTTPS, IMAP, LDAP, POP3, RTSP, SCP, SFTP, SMB, SMTP, TELNET and TFTP. There is no client interaction in the above method. cURL is highly featured command that supports services like user authentication, FTP uploads, HTTP posts, cookies etc. Since cURL supports wide range of file transfer protocols it can be used to transfer multiple files in single turn. This command can also be extensively used for automation purposes as there is no client involved.

How cURL interacts with the server?

The cURL can support the HTTPs protocol for its security check. Any remote server that requires connection gets connected through the https protocol and cURL undergoes a SSL certificate verification process to check the authentications of the remote server. cURL looks for the certificate files in the directories against the server certificate to ensure the remote server has a valid certification.


Syntax of cURL command

Curl has a basic syntax:-

curl [options] [URL…]

URL : Type curl in the command line a followed by the URL to retrieve. The URL’s content is displayed as the output for the terminal window mostly the desktop.

Progress Meter: When you run a curl command you have a facility to see the upload rate, download rate and the total data that is transferred using the progress meter.

Syntax-

$curl  ftp://ftp.bitarray.io/file.zip

$curl --silent ftp://ftp.bitarray.io/file.zip

 


Options used along with curl command

-o : If you want to redirect the output to a file on the device rather than displaying on the terminal window you use -o option in the command followed by the file name.

Syntax:            curl -o [file_name] [URL…]

 

-C : To resume downloading from the point where an interruption occurred you can use the –C- option which proves itself very useful in case of interruption during large amount of data download.

Syntax:            curl -C – [URL…]

 

limit-rate : If you want to limit your rate of data transfer you can set an upper bound by using limit-rate option and control the bytes usage.

Syntax:            curl –limit-rate [value] [URL]

Example:

$curl --limit-rate 1000K www.bitarray.io

 

-u : You can download files from FTP servers by using user authentication by using –u command syntax.

Syntax:           curl -u {username}:{password} [FTP_URL]

 


Difference between curl and wget

Both curl and wget are commands used for getting files or transferring files between servers. But there are many points on which the functionality of both of these differs. Like, one of the main advantages that Wget has when compared with Curl is that it has the ability to download recursively.

While curl requires libcurl library to implement it wget doesn’t require any library to support it. Talking about the range of protocols supported by them , Curl supports larger array of protocols which could be FTP, Gopher, HTTP, HTTPS, SCP, SFTP, TFTP, TELNET, LDAP, POP3, IMAP, SMB/CIFS, SMTP and RTSP while wget provides support on only few of them such as FTP, HTTP and HTTPS. You need to use separate curl command for separate data transfer while wget permits the use of recursive downloading. You can download everything that the remote server offers with a single command of wget but in curl the process is quite different and tine consuming. Curl can be used for both uploads and download while wget is used for the plain HTTP POST. As discussed before you need to use an option to redirect output to the local file in a curl command but Wget requires no extra options to do the same.

 


How to send GET/POST request using curl

GET– We use get method to derive information from a remote server using the URL of that server. The server receives the Hypertext Transfer Protocol by reading the file described by the URL. When the request is sent curl will send the response to that request with the body.

Classic Get request in curl looks like the following:-

$curl https://bitarray.io/

In curl the response header is hidden in the requested output by default and to see them you have to use –i option. This will give more information about the header.

$curl –i https://bitarray.io/

If you want to look at only the response header skipping the response body than you can use –I option in the command.

$curl -I https://bitarray.io/

 


cURL POST

When you want to send some additional data like about the file name or the user information along with the request to the server, then we use POST method. By default, the curl implements the GET method in the request. To replace the GET with POST you have to use option -X.

To send an HTTP POST request the following syntax is used:-

curl -X GET https://bitarray.io/

If a URL that is encoded is passed along with the post request the content-type is sent along with that.

Example:-

$curl -d "option=value&something=anothervalue" -X POST https://bitarray.io/

 


How to send cookie using curl

Apart from offering a variety of services curl can be used to define cookies and store them by simply activating the option which is inbuilt. To use the cookies in curl you have to explicitly turn the cookie engine on as curl does not consider cookies by default.

Being able to send and receive cookies with requests you use curl to read or write cookies. You enable the cookie engine by asking curl to read or write cookies.

Command-line options:

Reading cookies -b – This command reads cookies from a file. The filename is given after the option.

Syntax:                                  curl -L -b cookies.txt http://bitarray.io

 

Writing a cookie -c- Command to write cookies to the given file we use option –c.

Syntax:                                 curl -c cookie-jar.txt http://bitarray.io

 

New cookie session- -j, –junk-session-cookies – This command is used to start a new cookie session by terminating current sessions. It tells the user that a new session is started.

Syntax:                                 curl -j -b cookies.txt http://bitarray.io/

+ There are no comments

Add yours