Curl is a command-line tool that transfers data with URLs. Essentially, it's like a web browser, but instead of displaying a webpage visually, it shows you the raw data that the web server sends back. To use it, you generally open your terminal or command prompt and type curl
followed by the URL you want to access.
Basic Usage
The most fundamental way to use curl is to retrieve the content of a web page:
curl https://www.example.com
This command will fetch the HTML source code of https://www.example.com
and display it directly in your terminal. It's the equivalent of viewing the source code of a webpage in your browser.
Common Options and Examples
Curl offers a wide range of options to customize your requests. Here are some of the most common:
-
-I
or--head
: Retrieves only the HTTP headers of the response. This is useful for checking server status, content type, or other metadata without downloading the entire page.curl -I https://www.example.com
-
-v
or--verbose
: Provides verbose output, showing you details about the request and response, including headers, connection information, and more. This is invaluable for debugging.curl -v https://www.example.com
-
-o
or--output
: Saves the output to a file instead of displaying it in the terminal.curl -o example.html https://www.example.com
This saves the HTML of
https://www.example.com
to a file namedexample.html
. -
-X
or--request
: Specifies the HTTP request method (e.g., GET, POST, PUT, DELETE). The default is GET. To make a POST request:curl -X POST https://www.example.com/api/endpoint
-
-d
or--data
: Sends data in a POST request. Used with-X POST
.curl -X POST -d "param1=value1¶m2=value2" https://www.example.com/api/endpoint
-
-H
or--header
: Adds a custom header to the request. Useful for setting content types, authorization tokens, or other request-specific information.curl -H "Content-Type: application/json" -X POST -d '{"key": "value"}' https://www.example.com/api/endpoint
-
-u
or--user
: Provides authentication credentials (username and password).curl -u username:password https://www.example.com
Examples in a Table
Command | Description |
---|---|
curl https://www.example.com |
Fetches the content of the website. |
curl -I https://www.example.com |
Retrieves only the HTTP headers. |
curl -o myfile.html https://www.example.com |
Saves the website content to myfile.html . |
curl -X POST -d "name=John" https://api.example.com |
Sends a POST request with data to the API endpoint. |
curl -H "Authorization: Bearer TOKEN" https://api.example.com |
Sends a request with an authorization header. |
Advanced Usage and Considerations
Curl is a powerful tool with many more features, including support for cookies, proxies, SSL certificates, and more. Refer to the curl
man page (man curl
in your terminal) for a complete list of options. Security is important when using curl, especially when sending sensitive data. Always use HTTPS to encrypt communication and be cautious about storing or displaying authentication credentials.
In summary, curl
is a versatile command-line tool that allows you to make HTTP requests and retrieve data from web servers. Understanding its basic usage and options opens up a wide range of possibilities for testing APIs, automating tasks, and debugging web applications.