All-in-one Collaborative API Development Platform

API Design

API Documentation

API Debugging

API Mock

API Automated Testing

Sign up for free

Top 12 cURL Commands with Examples

Start for free
Home / Basic Knowledge / Top 12 cURL Commands with Examples

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.

Apidog excels in visually appealing API response documentation and user-friendly testing tools, including assertions and testing branches. It's an accessible and efficient solution for those seeking simplicity over complex 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 -i Command
The -i option in the Curl command is a gateway to detailed information about the HTTP response headers. By using this option, users can gain a deeper understanding of the server’s response and extract crucial metadata from the HTTP headers.

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.

Curl -h Command
One of the most essential options in the curl toolkit is the -h option, which enables users to manipulate and inspect HTTP headers.

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&param2=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.

Curl -v Command
curl is a powerful command-line tool for HTTP requests, supporting various protocols. The -v option activates verbose mode, revealing detailed communication logs between the client and server.

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 -u Command
The curl command, a versatile tool for making HTTP requests, offers the -u option to simplify the process of including user credentials in your requests. In this article, we’ll explore the curl -u command, its syntax, and how it enhances the efficiency of handling 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" \

The combination of -d and -H parameters ensures seamless transmission of specified data with the appropriate content type. Check below for details.

Curl Command: How to Use Curl-D Option
The ‘-d’ option in the Curl command is used to send data as part of an HTTP POST request. This option allows users to include specific data in the body of the request, enabling the transmission of information to a web server.

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.