Node.js code for Curl Examples ExampleThis Node.js 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 Usage Guide
Specify the -h command-line option to quickly get a list of useful command-line options with appropriate descriptions.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.