12 Practical Examples of Using Curl [Python Code]

This article provides 12 practical examples of using the Curl command-line tool, with a brief description of each Curl example. Curl examples include sending a JSON file to a server, submitting a web form, user authentication, proxy support, saving the server response to disk, and more. Most of the examples given can be done right in the browser using the ReqBin Online Curl Client. The Python code was automatically generated for the Curl Examples example.
12 Practical Examples of Using Curl [Python Code] Run
curl https://reqbin.com/echo
Updated: Viewed: 33357 times
Python code for Curl Examples example

Python code for Curl Examples Example

This Python code snippet was generated automatically for the Curl Examples example.
<< Back to the Curl Examples example

What is Curl?

Curl is a popular command-line utility and open-source and cross-platform library (libcurl) that users can send HTTP requests from clients to servers. Curl supports over 25 protocols, including HTTP, HTTPS, FTP, and runs on Windows, macOS, and Linux platforms. Curl has built-in support for SSL certificates, HTTP cookies, user authentication, proxies, and certificate validation.

Curl Commands Syntax

The general syntax for the Curl command for sending requests is as follows:

Curl General Syntax
curl [options] [URL]

Curl Usage Guide

Specify the -h command-line option to quickly get a list of useful command-line options with appropriate descriptions.

Curl Help
curl -h

1. Get Page Content

This example demonstrates the simplest Curl command. When no parameters are specified on the command line, Curl sends an HTTP GET request, and the command prints the HTTP response of the corresponding URL.

Curl GET Request Example
curl https://reqbin.com/echo

2. Send JSON Data to the Server

To send JSON data to the server, you need to set the request's Content-Type to application/json and pass the JSON data with the -d command line parameter. The JSON MIME type is set using the -H "Content-Type: application/json" command line parameter.

Curl Send JSON Example
curl -X POST [URL]
     -H "Content-Type: application/json" 
     -d "[JSON data]"

3. Submit Form Data to the Server

To send form data to the server, you can use the -d command-line options and pass the form data as key/value pairs. You can also use -F command line parameter and pass form data as name=value pairs.

Curl Submit Web Form Example
curl [URL] -d "key1=value1&key2=value2"
curl [URL] -F key1=value1 -F key2=value2 -F key2=@filename

4. Save Downloaded File to the Disk

To save the file to disk, you can use one of the following command-line options: -o and --output. The -O flag saves the downloaded file with the same name as in the URL. The --output filename flag saves the file with the specified name.

Curl Download File Example
curl -O [URL]

5. Send Cookies with Request

You can use the --cookie "name=value" command line parameter to send cookies to the server with your request. Curl will automatically convert this parameter to the Cookie: name=value request HTTP header.

Curl Send Cookies Example
curl --cookie "name=value" [URL]

6. Ignore SSL Certificate Errors

To ignore SSL certificate validation, you can pass the -k or --insecure option to the Curl command. This option tells curl to perform "unsecured" SSL connections and file transfers. Data is still transmitted over the SSL encrypted channel, but Curl ignores any security warnings about invalid or expired SSL certificates and accepts them as valid.

Curl Disable Certificate Validation Example
curl -k [URL]

7. Get only HTTP Headers

The -I or --head option allows Curl to fetch only a specific resource's HTTP headers (HTTP HEAD method). The HEAD request method is identical to the GET method, except that the server does not return the body of the HTTP message.

Curl Get Only HTTP Headers Example
curl -I https://reqbin.com/echo

8. Download Multiple Files at Once

You can download multiple files in a single Curl command by specifying the -O parameter multiple times with the desired URLs. If files are downloaded from the same server, Curl will try to reuse the connection.

Curl Download Multiple Files Example
curl -O [URL1] -O [URL2]

9. Do HTTP Authentication

Some websites may require user authentication before providing any content (username and password). You can pass user credentials to the server using the -u command line option. By default, Curl uses Basic HTTP Authentication. We can specify other authentication methods using –ntlm or –digest.

Curl HTTP Authentication Example
curl -u username:password [URL]

10. Follow Redirects

By default, Curl does not perform 300x redirects. To tell Curll to follow HTTP redirects, use the -L or --location command line option. The server provides the new address using the Location HTTP header.

Curl Follow Redirect Example
curl -L [URL]

11. Connect via Proxy Server

To establish a connection through a proxy server, pass the required proxy address using the -x command line parameter. You can pass the username and password for proxy authentication using the -U command line parameter.

Curl Proxy Example
curl -x [protocol://][host][:port] -U user:password [URL]

12. Make Curl Verbose

The -v option makes Curl verbose at runtime and asks Curl to provide detailed information about the progress of the operation, which can be useful when debugging complex situations.

Curl Verbose Example
curl -v [URL]

Summary

We've provided just 12 examples of the most commonly used use cases for Curl. But Curl's use is not limited to these use cases. Curl has about 380 flags that can be used to customize almost any aspect of Curl's operation.

See also

Generate code snippets for Python and other programming languages

Convert your Curl Examples request to the PHP, JavaScript/AJAX, Node.js, Curl/Bash, Python, Java, C#/.NET code snippets using the Python code generator.