Introduction
The curl
command in Linux is a powerful tool used for transferring data with URL syntax. Supporting a variety of protocols including HTTP, HTTPS, FTP, and SFTP, curl
is a must-know for developers, system administrators, and any users who need to interact with web servers or automate data transfer and API interactions. This guide will delve into the versatility of the curl
command, and its usage, and provide practical examples to illustrate its applications.
Understanding curl command in Linux
curl
, which stands for Client URL, is a command-line tool and library for transferring data using various network protocols. It’s known for its versatility and is used in command lines or scripts to transfer data. It is also used in cars, television sets, routers, printers, audio equipment, mobile phones, tablets, set-top boxes, media players, and is feature-rich, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, MQTT, POP3, POP3S, RTMP, RTMPS, RTSP, SCP, SFTP, SMB, SMBS, SMTP, SMTPS, Telnet, and TFTP. curl
is powered by libcurl for all transfer-related features.
Installation
curl
is usually pre-installed on most Linux distributions. However, if it’s not included in your setup, you can easily install it using your distribution’s package manager:
For Ubuntu/Debian:
sudo apt-get install curl
For CentOS/RHEL:
sudo yum install curl
For Fedora:
sudo dnf install curl
Basic Usage
The basic syntax of the curl
command is:
curl [options] [URL...]
To retrieve the contents of a web page:
curl http://example.com
This command will display the HTML content of the website in your terminal.
Options and Flags
curl
offers an extensive range of options that can be used to specify the exact operations you want to perform. Here are some of the most commonly used options:
-o
or--output
: Write output to a file instead of stdout.-I
or--head
: Fetch the HTTP-header only.-u
or--user
: Server user and password.-F
or--form
: Submit form data.-X
or--request
: Specify request command to use.-L
or--location
: Follow redirects.-H
or--header
: Extra header to include in the request.-d
or--data
: Send POST data.
Fetching Data
To save the output of the URL to a file, use:
curl -o filename.html http://example.com
Sending Data
To send data via a POST request with curl
:
curl -d "param1=value1¶m2=value2" -X POST http://example.com/login
Using Headers
To include an additional request header:
curl -H "X-Header: Value" http://example.com
Handling Redirects
To follow redirects sent by the server:
curl -L http://example.com
Advanced Usage
- Authenticating Requests: If accessing a resource requires authentication,
curl
can handle that using:arduinoCopy codecurl -u username:password http://example.com
- Working with APIs:
curl
is extensively used to interact with APIs. For a JSON API, you might use:jsonCopy codecurl -H "Content-Type: application/json" -d '{"key1":"value1", "key2":"value2"}' -X POST http://example.com/api
- Downloading Files:
curl
can also be used to download files:arduinoCopy codecurl -O http://example.com/file.tar.gz
File Upload
To upload a file to a server using curl
, you can use the -F
(form) option. This sends data as multipart/form-data, which is commonly used for file uploads in forms:
curl -F "file=@/path/to/local/file.txt" http://example.com/upload
This command uploads a file located at /path/to/local/file.txt
to the specified URL.
REST API request using curl command
GET Request
A GET request is used to retrieve data from a server. Here’s how you can perform a GET request with curl
:
curl http://example.com/api/data
To include headers with a GET request, for example, an authorization token, you can use:
curl -H "Authorization: Bearer YourTokenHere" http://example.com/api/data
POST Request
A POST request is commonly used to send data to a server to create a resource. Here’s how you can send a POST request with form data:
curl -d "username=user&password=pass" -X POST http://example.com/login
For JSON data, set the appropriate content type:
curl -H "Content-Type: application/json" -d '{"username": "user", "password": "pass"}' -X POST http://example.com/api/login
PUT Request
A PUT request is used to send data to a server to update or replace a resource. Here’s an example of a PUT request with curl
:
curl -d "username=newuser&password=newpass" -X PUT http://example.com/api/user/1
For JSON data:
curl -H "Content-Type: application/json" -d '{"username": "newuser", "password": "newpass"}' -X PUT http://example.com/api/user/1
DELETE Request
A DELETE request is used to delete a specific resource. Here’s how you can perform a DELETE request with curl
:
curl -X DELETE http://example.com/api/user/1
To include headers, such as an API key:
curl -H "API-Key: YourAPIKeyHere" -X DELETE http://example.com/api/user/1
Conclusion
curl
is an indispensable tool for anyone who works with the web. From simple data retrieval to complex API interactions, curl
provides the flexibility and tools needed to interact with different network protocols easily. Whether you’re testing APIs, downloading files, or automating interactions with websites, curl
offers a robust solution for all web-based communications.
Understanding curl
will not only expand your command-line prowess but also enhance your ability to automate and streamline your interactions with the web.