CURL
CURL
CURL
Transferring data from one place to another is one of the main task done using computers
connected to a network. There are so many GUI tools out there to send and receive data,
but when you are working on a console, only equipped with command line functionality,
using curl is inevitable. A less known fact is that curl can work with a wide range of
protocols and can solve most of your scripting tasks with ease. Before getting into details of
curl use cases and examples, let's see who is behind its development.
CURL comes by default installed in most of the distributions. If you do not have curl tool
installed, then its a single apt-get(apt-get install curl) or yum(yum install curl) command
away. Installing curl will install the curl command line tool as well as libcurl library. On an
Ubuntu system the package names are as follows.
1. curl_7.22.0-3ubuntu4.7_amd64.deb
2. libcurl3_7.22.0-3ubuntu4.7_amd64.deb
The above command will show the entire HTTP content on that example.com URL. Here
also curl tried to guess the protocol. But as it didn't find any, it defaulted to HTTP.
Under normal cases, you will have to specify the URI which includes the protocol information,
so that curl will use your desired protocol to fetch data. For example, protocol://prefix
The previously shown example command will not save the html output(It will show you the
output in the console itself.). If you want to save the output to a file, you can either use
redirection method in linux, or use -o option in curl.
?
The above command will save the output to example.html file. You can alternatively use -o
option in curl as shown below.
?
CURL command can be used to download multiple files at the same time, using -O option.
An important fact to note here is that, curl will use the same TCP connection to download
multiple files. This is done to improve the download speed. Establishing TCP connection to
a target server requires multiple processes. Its called as three way handshake. To reduce
the time involved in doing a three way handshake, curl will try to use the same connection
for all downloads from the same server issued by the single command.
The above command will download three different version of the package libiconv from
ftp.gnu.org(Please note the fact that all the three URL's are different.). Another advantage
of using -O option is that it will save the output to a file with the exact same name as the
URL file name. In our example, it will save all the three files with file name as libiconv-
1.10.tar.gz, libiconv-1.12.tar.gz,libiconv-1.13.tar.gz.
Google does this kind of redirection, if you try to do a CURL to google.com (instead of
www.google.com.). The HTTP response will contain two things. It will contain the status
code 301 or 302, and an alternative URL which the client can use. Web browsers will
automatically redirect you to the alternative URL, when they encounter 301, or 302. But in
case of curl, it will show you the exact message returned by the server. Let me show you
this..
?
If you want CURL to redirect automatically to the new URL then you need to use the -
L option as shown below.
?
Similar to any GUI download manager, you can pause and resume downloads using CURL.
This can be achieved using -C option as shown below. Let's first start a download and then
cancel it(with CTRL + C).
?
On resuming the transfer, it clearly says that its resuming from byte 28672. This is because
curl tried to fetch the bytes previously downloaded from the partially saved file libiconv-
1.14.tar.gz, in the current directory. And on finding the partially downloaded file, it resumed
from where it left last time.
This will show you the complete headers that curl encounters while fulfilling the request.
Headers include request headers sent, Response headers received etc.
Sometimes you only want to see the response headers returned by the server, without
seeing the actual response content. This can be used in scripts to verify the response status,
response bytes, or response server type etc.
1
2 root@ubuntu1:~# curl -I example.com
3 HTTP/1.1 200 OK
Accept-Ranges: bytes
4
Cache-Control: max-age=604800
5 Content-Type: text/html
6 Date: Mon, 17 Mar 2014 12:42:14 GMT
7 Etag: "359670651"
8 Expires: Mon, 24 Mar 2014 12:42:14 GMT
Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
9 Server: ECS (iad/19AB)
10 X-Cache: HIT
11 x-ec-custom-error: 1
12 Content-Length: 1270
13
Use Proxy Server with CURL
If you want to send your request through a proxy server, then you can do that with -
x option as shown below. Somtimes these proxy servers require authentication first. Let's
see how to perform a curl request with proxy authentication.
?
In the above command, k stands for kilo bytes. You can also use bytes by using B,
megabytes by using m, gigabytes using G.
The above shown command will download the file myfile.zip from
ftp://example.com/mydirectory, and save it as myfile.zip. But if you want to first see the
directory structure of your FTP server, then you can do that by the following method.
?
1 root@ubuntu1:~# curl ftp://example.com --user username:password
Above command, will list the directory and files inside your users doc root.
Uploading files to FTP server using CURL
Uploading can be done using -T option in curl, and is very simple and straight forward. Its
shown below.
You can also delete files on your FTP server using curl command, as shown below.
?
In the above command, replace smtps://smpt.example.com:465 with your SMTP server and
port.
--mail-from: This field contains the from address that the receiver should see.
--mail-rcpt: This field contains TO address
--upload-file: The file provided here should contain your message as a content
--user: SMTP user@domain:password
--insecure option used is exactly same as using -k option we saw earlier, to ignore unknown
SSL certificates.
Using CURL to send HTTP POST, PUT, DELETE requests
Apart from the above seen examples, curl can be used to send different types of HTTP
request methods to your server. This becomes very much handy if you want to configure an
application using REST API. I will show you some example's of sending REST API requests
using CURL.
In the above example, PUT is the request method, -u is used to mention credentials to
access that specific resource on the server, -H content-type is the type of content format
that we will be sending to the server (can be xml, normal text etc). -d with
@/home/user/file.xml indicates to send the content of file.xml to the server. This file will
contain the configuration options with correct syntax that the
URL http://example.com/myconfigs/status will accept.
Sending PUT request is exactly same as the above shown example of POST. Simply replace
POST with PUT in the above example. If your web server does not accept these methods,
you will get a 405 error as reply. HTTP 405 means that the server does not accept the HTTP
method on that specific URL.
You can also get HTTP unsupported Media Type error as reply, if the server does not accept
application/xml format. The HTTP status code for unsupported media type is 415.
Using the above command, you can send cookies saved in the file (mycookies.txt in our
case) as input to the server. Alternatively you can also use the below method to send
cookies.
If there is no = sign in the parameter passed to -b switch, then it is considered as a file, from
where cookies should be read as input(basically the previous method we saw)
1
2 Accept-Ranges: bytes
Cache-Control: max-age=604800
3
Content-Type: text/html
4 Date: Tue, 18 Mar 2014 15:42:32 GMT
5 Etag: "359670651"
6 Expires: Tue, 25 Mar 2014 15:42:32 GMT
7 Last-Modified: Fri, 09 Aug 2013 23:54:35 GMT
Server: ECS (iad/19AB)
8 X-Cache: HIT
9 x-ec-custom-error: 1
10 Content-Length: 1270
11
All of the above shown stuff returned by a server are part of HTTP headers. You can modify
the HTTP request headers using CURL. Although whether its a good feature of bad requires
a serious debate, coz modifying headers can fool a web server receiving the request. Due to
this the data found in headers can never be trusted. Let's see how to modify headers while
sending request.
-H option in curl can be used to modify headers. You can modify any header of your wish.
For example, let's modify our Accept and Content-type headers in the request.
We saw the method to ignore the SSL certificate verification with -k option. But what if you
want to verify the certificate the server is replying with. In that case, you need to provide
the CA (Certificate Authority Certificate) to curl. This can be done by a simple command
line option called --cacert.
The above command will download the file myfile.gz, if its modified after 3rd jan 2014.
Hope these examples were helpful.