Top 12 cURL Commands with Examples
cURL offers powerful features for HTTP/HTTPS requests. Ideal for automation, these commands facilitate user authentication, proxy support, and more. This guide will help you learn how to use various options with curl commands.
cURL, a versatile command-line utility for data transfer across various protocols, offers powerful features for HTTP/HTTPS requests. Ideal for automation, these commands facilitate user authentication, proxy support, and more. You can run cURL commands on Windows, Mac, and Linux. This guide will help you learn how to use various options with curl commands.
Curl -i Command: Check Page HTTP Headers
Examine the HTTP headers of a page using the -I
parameter for an HTTP HEAD request:
$ curl -I https://example.com/page
This insightful command fetches only the HTTP headers, making it efficient for checking URL headers without loading the entire page content.
Curl -o Command: Save URL Content to a File
To effortlessly store URL content, utilize the -o
command-line option:
$ curl -o output_file1.txt https://example.com/resource1
The -o
option directs cURL to save the URL content in the current working directory with the specified filename. This facilitates easy downloads using the GET request method.
Curl -v Command: Download Multiple Files at Once
Efficiently download multiple files by passing a list of URLs to cURL using the -O
command-line option:
$ curl -O https://example.com/resource2 \
-O https://example.com/resource3 \
-O https://example.com/resource4
The -O
option ensures that cURL saves each downloaded resource with its original filename, streamlining the process.
Force Curl to Use HTTP/2 Protocol:
Ensure optimal performance by compelling cURL to use the HTTP/2 protocol with the --http2
option:
$ curl --http2 https://example.com
Combined with the -I
command-line parameter, this command helps verify a website's support for the HTTP/2 protocol.
Curl -L Command: Follow Redirects
Enable cURL to automatically follow redirects by employing the -L
option:
$ curl -L //www.example.com/redirected-page
This is particularly useful as it instructs cURL to handle HTTP redirects, enhancing the completeness of your requests.
Curl -x Command: Use Proxy Server
For scenarios involving proxy servers, establish connections seamlessly with the -x
command-line option:
$ curl -x proxy.example.com:8080 -U user:password https://example.com
The -U
command-line option conveniently passes the proxy username and password to ensure secure and authenticated requests.
Additional cURL Commands for Advanced Use:
Now, let's explore more advanced cURL commands that add depth to your toolkit.
Curl -h Command: Additional HTTP Headers with Request
Enhance request customization by adding extra HTTP headers using the -H
command-line option:
$ curl -H "Authorization: Bearer token123" https://example.com/api/resource
This flexibility allows you to include multiple HTTP headers for tailored communication with the server.
Mastering cURL -v for HTTP Requests with Cookies
The -v
option stands for "verbose," and when included, it provides detailed information about the request and response, helping you troubleshoot and understand the communication between your machine and a server.
You can also use curl -v
with other HTTP methods, such as POST. Here's an example:
curl -v -X POST -d "param1=value1¶m2=value2" https://www.example.com/api
In this example, the command sends a POST request to "https://www.example.com/api" with the specified data parameters.
Secure API Access with cURL -u
The curl -u
command is used for making HTTP requests with authentication. The -u
option is followed by a username and an optional password, separated by a colon. This allows you to include credentials in your request.
Here's a simple example:
bashCopy code
curl -u username:password https://api.example.com/resource
In this example, the command sends a GET request to "https://api.example.com/resource" with the provided username and password for authentication.
Curl -d Option: Send Data to the Server
Efficiently send data to the server using the -d
command-line parameter for HTTP POST requests:
$ curl -d '{"key": "value"}' \
-H "Content-Type: application/json" \
https://example.com/api/post-endpoint
The combination of -d
and -H
parameters ensures seamless transmission of specified data with the appropriate content type. Check below for details.
Change the User-Agent String:
Customize the User-Agent string for your requests using the --user-agent
command-line option:
$ curl --user-agent "MyApp/2.0" https://example.com
This command allows you to tailor the User-Agent string, which can be beneficial when certain browsers are expected by the server.
Curl -b Command: Send Cookies to Website
Incorporate cookie handling seamlessly by using the -b
command-line parameter:
$ curl -b "sessionid=abc123; user=JohnDoe" https://example.com
The -b
option facilitates sending cookies back to the website, either by specifying a file or passing them directly as a string.
By integrating these enhanced cURL commands into your workflow, you elevate your efficiency in handling a variety of scenarios, making cURL an indispensable tool for seamless interactions with servers.