Top 12 Curl Commands with Examples [Node.js Code]

Curl is a popular command-line utility for transferring data to or from a server using over 25+ protocols. The Curl command-line tool provides several advanced options such as user authentication, proxy support, resuming transmission, limiting bandwidth and transfer rates, and more. Curl commands work without user interaction and are therefore ideal for use in automation scenarios. This article will go over the 12 most essential Curl commands for day-to-day use for making requests over HTTP/HTTPS protocols. The Node.js code was automatically generated for the Curl Commands example.
Top 12 Curl Commands with Examples [Node.js Code] Run
Updated: Viewed: 33732 times
Node.js code for Curl Commands example

Node.js code for Curl Commands Example

This Node.js code snippet was generated automatically for the Curl Commands example.
<< Back to the Curl Commands example

How to install Curl?

You can install Curl on Linux, Windows, and macOS in the following ways:

Install Curl on Linux

On Linux, enter the following command to install Curl:

Install Curl on Linux Example
$ sudo apt install curl
$ sudo apt-get install curl

Install Curl on Windows

To use Curl on Windows, download the installer from the Curl official website and unpack the archive to the desired local folder on your computer. Add the Curl folder (usually C:\Curl\bin) to your Windows PATH environment variable to invoke the Curl command from anywhere else. Enter curl --version on the command line to make sure you can use Curl on Windows.

Check Curl version
curl --version

Install Curl on macOS

Curl comes with macOS already. You can update Curl to the latest version by installing macOS Homebrew Software Package Manager. After installing Homebrew, open a Terminal and type:

Install Curl on macOS Example
brew install curl

How to use the Curl command-line tool?

The Curl command syntax is protocol-dependent. For the HTTP protocol, the Curl command line syntax is as follows:

Curl syntax
curl [options] [URL]

Below is a list of the 12 best Curl commands that you will use the most during your work.

1. Get resource content by URL

Curl command to get the content of a page

If no parameters are specified, Curl sends an HTTP GET request to the server and outputs the resource's content (for example, the HTML code of a page) to standard output (usually a terminal window).

2. Save URL content to a file

Curl command to save URL content to a file
curl -o logo.png

The Curl -o command-line option tells Curl to save the contents of the URL in the current working directory with the specified file name. The resource is downloaded with the GET request method.

3. Download multiple files at once

Curl command to download multiple files
curl -O 

The list of URLs is passed to Curl with one of the -o or -O command-line options. As we saw in example #2, for the command line parameter -o, you can pass the file name under which the resource will be saved, and the -O command line parameter tells Curl to save the downloaded resource with the original file name.

5. Check Page HTTP headers

Curl command to check page HTTP headers
curl -I

The -I parameter tells Curl to send an HTTP HEAD request to the server instead of a GET. The HEAD request is similar to a GET, except that the server only returns HTTP headers. This is useful if you only want to check the URL headers and not load the page content (to save Internet traffic).

6. Force Curl to use HTTP/2 protocol

Curl command to use HTTP/2 protocol
curl --http2

The --http2 option forces Curl to use the HTTP/2 protocol instead of HTTP/1.1. Combined with the -I command line parameter, we can use this Curl command to check if a website supports HTTP/2.

7. Do Follow Redirects

Curl command to follow redirects
curl -L

By default, Curl does not follow redirects (HTTP status codes 301 and 302). The -L option tells Curl to do HTTP redirects.

8. Use proxy server

Curl command to connect via a proxy server
curl -x -U user:password

If you are behind a proxy server, the -x command-line option will allow you to send requests through the proxy server. The -U command-line option passes the proxy username and password to the proxy.

9. Provide additional HTTP headers with request

Curl command to send additional HTTP headers to server
curl -H "Accept: application/json"

The -H command-line option allows additional HTTP headers to be passed to the server. You can provide as many HTTP headers as you want using the -H option multiple times.

10. Send data to the server

Curl command to sending data to the server
curl -d '{"id": 123456}'
   -H "Content-Type: application/json"

The -d command line parameter tells Curl to send the supplied data to the server using the HTTP POST request method. The -d option is usually followed by the -H parameter to indicate the data type.

11. Change the User-Agent string

Curl command to change the User-Agent string
curl --user-agent "MyAppName 1.0"

The --user-agent command-line option allows you to pass any string to be used instead of the standard Curl User-Agent string. This can be useful if the server only expects requests from certain browsers.

12. Send Cookies to Website

Curl command to send cookies with the request
curl -b "name1=value1; name2=value2"

The -b command line parameter allows you to send cookies back to the website. You can use the -b switch with the file's name that contains the cookies or pass the cookies in the string.

See also

Generate code snippets for Node.js and other programming languages

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