curl
Command in Linux
Summary
curl
is a command-line tool used to transfer data with URL syntax, supporting protocols like HTTP, HTTPS, FTP, and more. It's often used for downloading files, testing APIs, and debugging web servers.
Introduction
curl
stands for "Client URL". It's a versatile tool that can be used to send and receive data over a network. It's particularly useful for interacting with web servers, whether you're fetching web pages, submitting forms, or testing API endpoints. It comes pre-installed on many Linux distributions and is available for installation on most others. Unlike a web browser, curl
operates from the command line and doesn't render the content it retrieves. It simply outputs the raw data or saves it to a file.
Use case and Examples
Download a file
This command downloads the filefile.zip
from https://example.com
and saves it to the current directory with the same name. The -O
(capital O) flag tells curl
to use the filename from the URL. Download a file with a different name
This command downloads the filefile.zip
from https://example.com
and saves it to the current directory as new_file.zip
. The -o
(lowercase o) flag specifies the output filename. Send a POST request with data
This command sends a POST request tohttps://example.com/api/endpoint
with the data param1=value1¶m2=value2
in the body of the request. The -d
flag specifies the data to be sent. Send a POST request with JSON data
curl -H "Content-Type: application/json" -d '{"param1": "value1", "param2": "value2"}' https://example.com/api/endpoint
https://example.com/api/endpoint
with JSON data. The -H
flag sets the Content-Type
header to application/json
, and the -d
flag specifies the JSON data. View HTTP headers
This command retrieves and displays only the HTTP headers from the response ofhttps://example.com
. The -I
flag stands for "head" and retrieves only the headers. Commonly used flags
Flag | Description | Example |
---|---|---|
-O | Save the downloaded file with the name from the URL. | curl -O https://example.com/image.jpg |
-o | Save the downloaded file with a specified name. | curl -o my_image.jpg https://example.com/image.jpg |
-I | Show the HTTP headers of a website. | curl -I https://example.com |
-v | Verbose mode, shows more information about the request. | curl -v https://example.com |
-H | Add a custom header to the request. | curl -H "Content-Type: application/json" https://example.com/api |
-d | Send data in a POST request. | curl -d "name=John&age=30" https://example.com/submit |
-u | Provide username and password for authentication. | curl -u user:password https://example.com/protected |
-k | Allows curl to perform "insecure" SSL connections and transfers. Useful for testing with self-signed certificates. | curl -k https://example.com |
-X | Specifies a custom request method. | curl -X DELETE https://example.com/resource/123 |