Bash curl
- Transfer a URL
Using the curl
Command
The curl
command is used to transfer data from or to a server using various protocols like HTTP, HTTPS, FTP, and more.
It's a versatile tool for downloading files, testing APIs, and more.
Basic Usage
To retrieve a web page, use curl url
:
Example
curl http://example.com/file.txt
Hello, this is a test file.
There are three lines here.
This is the last line.
Options
The curl
command has options to change how it works:
-O
- Save the file with the same name as the remote file-L
- Follow redirects-I
- Fetch the HTTP headers only-d
- Send data with POST request-u
- Specify user and password for server authentication
Save the File with the Same Name as the Remote File
The -O
option allows you to save the file with the same name as the remote file.
This is useful for downloading files directly to your local system with their original names.
Example: Save the File with the Same Name as the Remote File
curl -O http://example.com/file.txt
% Total % Received % Xferd Average Speed Time Time Time Current
Dload Upload Total Spent Left Speed
100 134 100 134 0 0 216 0 --:--:-- --:--:-- --:--:-- 218
Follow Redirects
The -L
option allows you to follow redirects.
This is useful when accessing URLs that may redirect to another location.
Example: Follow Redirects
curl -L http://example.com/redirect
Hello, this is a test file.
There are three lines here.
This is the last line.
Fetch the HTTP Headers Only
The -I
option allows you to fetch the HTTP headers only.
This is useful for checking server response headers without downloading the entire content.
Example: Fetch the HTTP Headers Only
curl -I http://example.com
# Output:
# HTTP/1.1 200 OK
# Date: Wed, 10 Apr 2025 10:00:00 GMT
# Content-Type: text/html; charset=UTF-8
# Connection: keep-alive
Send Data with POST Request
The -d
option allows you to send data with a POST request. This is useful for submitting form data or interacting with APIs.
Example: Send Data with POST Request
curl -d "fname=John" https://www.example.com/action_page.php
Submitted Form Data
Your input was received as:
fname=John
Specify User and Password for Server Authentication
The -u
option allows you to specify a user and password for server authentication. This is useful for accessing protected resources.
Example: Specify User and Password for Server Authentication
curl -u user:password http://example.com/protected
Hello, this is a test file.
There are three lines here.
This is the last line.
Understanding curl
Output
The curl
command output will vary depending on which options are used:
- HTTP Status Code: Indicates the success or failure of the request.
- Response Headers: Provide metadata about the server response.
- Response Body: The actual content retrieved from the server.
- Progress Meter: Shows download progress and speed.