Using Curl Command-line Tool [Python Code]

Curl is one of the best tools for debugging network requests and testing APIs without user interaction. Curl can be used as a standalone console application or compiled into other programs as a dynamic library (libcurl). Curl has built-in support for SSL, user authentication, certificate validation, and HTTP cookies. You can use Curl to download or upload files, submit web forms, send requests to API endpoints, and simulate user actions without using a web browser. The Python code was automatically generated for the How Use Curl example.
Using Curl Command-line Tool [Python Code] Run
curl https://reqbin.com/echo
Updated: Viewed: 35767 times
Python code for How Use Curl example

Python code for How Use Curl Example

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

What is Curl?

Curl is a command-line tool that allows you to transfer data over the network using over 25+ protocols, including HTTP, HTTPS, FTP, and works on Linux, macOS, and Windows platforms. Curl is free and open-source, has an excellent distribution license agreement, can be used in commercial or closed source applications, and is widely used in many popular applications and browsers.

How to use Curl on Windows?

To use Curl on Windows, follow these steps:

  1. Download Windows Installer from Curl official website (64-bit recommended).
  2. Open the Curl.zip folder and unzip it into the desired local folder on your computer, for example, C:\Curl.
  3. Add the Curl folder (C:\Curl\bin) to your Windows PATH environment variable to invoke the Curl command from any other folder.
  4. Enter curl --version on the command line to make sure you can Curl commands. If Curl is installed correctly, you will see something like this:
Check Curl installation on Windows
curl --version
curl 7.55.1 (Windows) libcurl/7.55.1 WinSSL
Release-Date: 2017-11-14, security patched: 2019-11-05
Protocols: dict file ftp ftps http https imap imaps pop3 pop3s smtp smtps telnet tftp
Features: AsynchDNS IPv6 Largefile SSPI Kerberos SPNEGO NTLM SSL

How to use Curl on macOS?

Curl comes bundled with macOS. You can update Curl to the latest version by installing macOS Homebrew Software Package Manager. Once you've installed the Homebrew Package Manager on macOS, open a terminal and type:

Install Curl on macOS
brew install curl

If Curl is installed correctly, you will see something like this when you type curl --version:

Check Curl installation on macOS
curl --version
curl 7.31.0 (x86_64-apple-darwin12.4.0) libcurl/7.31.0 OpenSSL/0.9.8x zlib/1.2.5
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtsp smtp smtps telnet tftp 
Features: IPv6 Largefile NTLM NTLM_WB SSL libz

How to use Curl on Linux?

You must enter the following command in a terminal to install Curl on Ubuntu/Debian Linux:

Install Curl on Ubuntu or Debian Linux
sudo apt install curl
or
sudo apt-get install curl

You can verify that Curl is installed and working correctly on Linux by running the following curl --version command in a terminal. If Curl is installed correctly, you will see something like this:

Check Curl installation on Linux
curl --version
curl 7.58.0 (x86_64-pc-linux-gnu) libcurl/7.58.0 OpenSSL/1.1.1 zlib/1.2.11 libidn2/2.0.4 libpsl/0.19.1 (+libidn2/2.0.4) nghttp2/1.30.0 librtmp 2.3
Release-Date: 2018-01-24
Protocols: dict file ftp ftps gopher http https imap imaps ldap ldaps pop3 pop3s rtmp rtsp smb smbs smtp smtps telnet tftp
Features: AsynchDNS IDN IPv6 Largefile GSS-API Kerberos SPNEGO NTLM NTLM_WB SSL libz TLS-SRP HTTP2 UnixSockets HTTPS-proxy PSL

Basic Curl Syntax

To send a request using Curl, use the following syntax:

Curl Syntax
curl [URL]

Curl Command-Line Syntax

Curl commands are used with the flags (- or --), use the following syntax:

Curl Syntax using option
curl [OPTIONS] [URL]

Basic Curl Parameters

These are not all Curl parameters, but here are the main ones you will need to use.

Options Сommands
-A Specify your USER_AGENT
-b Save a cookie to file
-c Send a cookie to the server from a file
-C Continue loading the file from the break or the specified offset
-d Send data using the POST method
-E Use an external SSL certificate
-F Send data as a form
-G If this option is enabled, then all data specified in the -d option will be transferred using the GET method
-H Send headers to the server
-I Receive only the HTTP header, and ignore the entire page content
-L Accept and process redirects
-o Output the content of the page to a file
-O Save the content to a file with the name of the page or file on the server
-p Use proxy
-v Maximum verbose output

How to download files using Curl?

Curl can download files from a remote location. Curl can do this with the command line options -O (save the file with the same name as the remote one) or -o (allow a different filename or location).

Curl using -O command-line
curl -O [URL]

Curl using -o command-line
curl -o [file name] [URL]

How to send POST requests data using Curl?

Using the method, you can send files and any data. This method also sends data of various . Use the -d option to send such a request. Using the POST method, you can send files and any data. This method also sends data of various forms. Use the -d option to send such a request. Below is a Curl example with a command line option -d:

Curl using -d command-line
curl -d "key1=value1&key2=value2" [URL]

How to get HTTP headers using Curl?

An HTTP request invariably contains a header. The HTTP headers send additional information about the remote web server along with the actual request. With the tools in the browser, you can see the header information and check it with the Curl command.

Curl using -I command-line
curl -I [URL]

How to send cookies using Curl?

If you have HTTP cookies in the file, you can send them to the site, and you can also use the utility to check which cookies are loaded by the URL.

Curl using --cookie command-line
curl --cookie "Name=Value" [URL]

How do I get HTTP Authentication using Curl?

If the server requires authentication of one of the common types, for example, HTTP or FTP, then Curl can handle this task. To specify authentication data, specify them separated by colons in the -u option:

Curl using -u command-line
curl -u [Username:Password] -T [ftp://URL]

How to upload files for FTP connections?

Curl supports FTP protocol. You can use it to download files from a remote server, and you can omit the username and password for anonymous FTP connections.

Curl using FTP
curl -u [Username:Password] -O [ftp://URL]

How to transfer files to Curl?

In addition to uploading files, the utility allows you to perform other actions, for example, sending files to an FTP server. There is a -T option for this:

Curl using -T command-line
curl -T page.html [URL]

How do I use a proxy to connect to Curl?

To use a proxy server to download files, you need to specify the proxy server address in the -x option:

Curl using -x command-line
curl -x "[protocol://][host][:port]" [URL] [options]

How to set timeout for Curl command?

To set a timeout for a Curl command, you can use the --connect-timeout parameter to set the maximum time in seconds that you allow Curl to connect to the server, or the --max-time (or -m) parameter for the total time in seconds that you authorize the whole operation.

Curl –connect-timeout syntax
curl -m [SECONDS] [URL]

Curl -max-time syntax
curl --connection-timeout [SECONDS] [URL]

How to limit download speed in Curl?

When downloading or uploading data using Curl, you have no way of knowing how large the output will be. You can limit your download speed to make sure Curl is not restricting your bandwidth. The command below will cap the download speed to 100K:

Curl using --limit-rate command-line
curl --limit-rate 100k -O [URL]

Conclusion

Curl is a powerful yet easy-to-use tool for automating day-to-day tasks and testing APIs. Curl has over 380 flags that you can use to customize almost any aspect of Curl. Libcurl is a free open source cross-platform library with an extensive API that can be used in many popular programming languages, supports over 25+ protocols. And works on all modern platforms.

See also

Generate code snippets for Python and other programming languages

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