Linux curl examples.

Introduction

In this post you will learn about curl command in Linux and then have a look at some common curl examples.

What is curl?

Curl is a command-line utility used for sending and getting data using URLs. curl supports various protocols, including HTTP, HTTPS, FTP, SCP, SFTP, TFTP, LDAP, TELNET, POP3, SMTP and others. It is often used by web developers for crafting various HTTP requests. I will use Ubuntu 18.04 for demo purposes, but you can use any Linux distribution.

How to install curl in Ubuntu?

Before we go through some curl examples we need to install it. The following command installs curl in Ubuntu:

$ sudo apt install curl

 

Example 1. curl version

You can run the following command to find the exact version of curl and which protocols it supports:

$ curl --version

curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.0g zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp/2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL

 

Example 2. GET request

You can download a file from the http server and print its contents to STDOUT, using a simple GET request:

$ curl http://example.com

You can save the output instead of printing it, by redirecting the output to a file,  like that:

$ curl http://example.com > example_com.txt

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1270 100 1270 0 0 3836 0 --:--:-- --:--:-- --:--:-- 3836

Another way to download a file is to specify a full path to it and use -O option:

$ curl -O http://example.com/index.html

% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 1270 100 1270 0 0 3825 0 --:--:-- --:--:-- --:--:-- 3825

 

Example 3. View HTTP response headers

You can use -I option to view headers of HTTP response:

$ curl -I example.com

HTTP/1.1 200 OK
Content-Encoding: gzip
Accept-Ranges: bytes
Cache-Control: max-age=604800
Content-Type: text/html; charset=UTF-8
Date: Wed, 14 Nov 2018 12:59:14 GMT
Etag: "1541025663+gzip"
Expires: Wed, 21 Nov 2018 12:59:14 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (dca/2498)
X-Cache: HIT
Content-Length: 606

 

Example 4. POST request

It is also possible to issue POST request with specified parameter “name=Orkhan”, as shown below:

$ curl --data "name=Orkhan" http://example.com

 

Example 5. Follow redirect link

You can use -L option to follow redirects. For example, if you try to retrieve Google’s homepage without -L option you will only get this:

$ curl http://google.com

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

Now, if you specify -L option you will get the desired destination page:

$ curl -L http://google.com

Example 6. Download a file from FTP server

The following command shows the contents of a remote FTP directory:

$ curl -u administrator  ftp://192.168.37.79
Enter host password for user 'administrator':
drwxr-xr-x    2 1000     1000         4096 May 22  2018 Desktop
drwxr-xr-x    2 1000     1000         4096 May 22  2018 Documents
drwxr-xr-x    3 1000     1000         4096 Nov 15 13:59 Downloads
drwxr-xr-x    2 1000     1000         4096 May 22  2018 Music
drwxr-xr-x    2 1000     1000         4096 May 22  2018 Pictures
drwxr-xr-x    2 1000     1000         4096 May 22  2018 Public
drwxr-xr-x    2 1000     1000         4096 May 22  2018 Templates
drwxr-xr-x    2 1000     1000         4096 May 22  2018 Videos
-rw-r--r--    1 1000     1000         8980 May 22  2018 examples.desktop
-rw-r--r--    1 1000     1000           30 Nov 25 16:28 test.txt

You can download a file from the FTP server using the following command:

$ curl -u administrator -O ftp://192.168.37.79/test.txt
Enter host password for user 'administrator':
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    30  100    30    0     0    258      0 --:--:-- --:--:-- --:--:--   258

-u – used to specify the username for our FTP connection

-O – downloads the specified file to the current working directory

 

Example 7. Upload a file to FTP server

You can also upload a file to a FTP server, using the options -u and -T:

$ curl -u administrator  -T ./backup.txt ftp://192.168.37.79
Enter host password for user 'administrator':
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100    12    0     0  100    12      0     70 --:--:-- --:--:-- --:--:--    70

-u – specifies username which should be used to connect to FTP server

-T – specifies that we want to transfer/upload the file to FTP server

 

Conclusion.

This post gave you an overview of using curl command in Linux.

Tags:,

Add a Comment