How to use the command 'curl' (with examples)

How to use the command 'curl' (with examples)

Curl is a widely used command-line tool that allows you to transfer data to or from a server. It supports various protocols, including HTTP, FTP, and POP3. Curl is a versatile tool that can be used for a variety of purposes, such as downloading files from a URL, sending form-encoded data, and authenticating with a server.

Use case 1: Download the contents of a URL to a file

Code:

curl http://example.com --output path/to/file

Motivation:

Downloading the contents of a URL to a file can be useful for various tasks, such as saving HTML code for analysis or storing data for future reference. By using curl, you can easily fetch the contents of a URL and save it to a specified file location.

Explanation:

  • curl: The command itself
  • http://example.com: The URL of the file you want to download
  • --output: Specifies the output file
  • path/to/file: The path and filename where the downloaded file will be saved

Example output:

The contents of the URL http://example.com will be downloaded and saved to the file specified in the --output parameter.

Use case 2: Download a file, saving the output under the filename indicated by the URL

Code:

curl --remote-name http://example.com/filename

Motivation:

Sometimes, you may want to download a file and preserve the original filename specified by the URL. By using the --remote-name option, curl will save the file with the same name as indicated by the URL.

Explanation:

  • curl: The command itself
  • --remote-name: Saves the output under the filename indicated by the URL
  • http://example.com/filename: The URL of the file you want to download

Example output:

The file located at http://example.com/filename will be downloaded and saved with the same name.

Use case 3: Download a file, following location redirects, and automatically continuing a previous file transfer, returning an error on server error

Code:

curl --fail --remote-name --location --continue-at - http://example.com/filename

Motivation:

This use case is helpful when you want to make sure that the file is downloaded correctly and any errors are reported. By using the combination of --fail, --location, and --continue-at -, curl will follow any location redirects, resume a previous file transfer, and return an error if the server encounters any issues.

Explanation:

  • curl: The command itself
  • --fail: Returns an error on server error (non-successful HTTP codes)
  • --remote-name: Saves the output under the filename indicated by the URL
  • --location: Follows any location redirects
  • --continue-at -: Automatically continues (resumes) a previous file transfer. The hyphen after continue-at specifies to continue from where it left off
  • http://example.com/filename: The URL of the file you want to download

Example output:

If the file transfer encounters any server error, it will return an error. Otherwise, the file will be downloaded, following location redirects and continuing a previous transfer if applicable.

Use case 4: Send form-encoded data (POST request of type application/x-www-form-urlencoded)

Code:

curl --data 'name=bob' http://example.com/form

Motivation:

Sending form-encoded data is useful for submitting data to a server, typically through a form submission. By using the --data option, you can specify the data to be sent in the HTTP request body.

Explanation:

  • curl: The command itself
  • --data 'name=bob': Specifies the form-encoded data to be sent
  • http://example.com/form: The URL where the data should be sent

Example output:

The form-encoded data “name=bob” will be sent to the URL http://example.com/form .

Use case 5: Send a request with an extra header, using a custom HTTP method

Code:

curl --header 'X-My-Header: 123' --request PUT http://example.com

Motivation:

Sometimes, you may need to send a request with additional headers or use a custom HTTP method. By using the --header option, you can include custom headers, and the --request option allows you to specify the HTTP method to be used.

Explanation:

  • curl: The command itself
  • --header 'X-My-Header: 123': Specifies the extra header to include in the request
  • --request PUT: Specifies the HTTP method to be used (in this case, PUT)
  • http://example.com: The URL where the request should be sent

Example output:

A request with the specified extra header and HTTP method will be sent to the URL http://example.com .

Use case 6: Send data in JSON format, specifying the appropriate content-type header

Code:

curl --data '{"name":"bob"}' --header 'Content-Type: application/json' http://example.com/users/1234

Motivation:

Sending data in JSON format is common when interacting with APIs that expect JSON payloads. By using the --header option to specify the content-type and the --data option to provide the JSON data, you can easily send JSON payloads to the server.

Explanation:

  • curl: The command itself
  • --data '{"name":"bob"}': Specifies the JSON data to be sent
  • --header 'Content-Type: application/json': Sets the appropriate content-type header
  • http://example.com/users/1234: The URL where the request should be sent

Example output:

The JSON data ‘{“name”:“bob”}’ will be sent to the URL http://example.com/users/1234 with the appropriate content-type header.

Use case 7: Pass a username and password for server authentication

Code:

curl --user myusername:mypassword http://example.com

Motivation:

Sometimes, you may need to authenticate with a server using a username and password. By using the --user option, you can provide the credentials required for authentication.

Explanation:

  • curl: The command itself
  • --user myusername:mypassword: Specifies the username and password for authentication
  • http://example.com: The URL where the request should be sent

Example output:

A request will be sent to the URL http://example.com with the provided username and password for authentication.

Use case 8: Pass client certificate and key for a resource, skipping certificate validation

Code:

curl --cert client.pem --key key.pem --insecure https://example.com

Motivation:

In some cases, you may need to pass a client certificate and key for authentication with a server. By using the --cert and --key options, you can provide the required credentials. The --insecure option allows you to skip certificate validation if necessary.

Explanation:

  • curl: The command itself
  • --cert client.pem: Specifies the path to the client certificate file
  • --key key.pem: Specifies the path to the client key file
  • --insecure: Skips certificate validation
  • https://example.com: The URL where the request should be sent

Example output:

A request will be sent to the URL https://example.com , providing the client certificate and key. Certificate validation will be skipped if the --insecure option is used.

Conclusion:

Curl is a powerful command-line tool that allows you to transfer data to or from a server, supporting various protocols. By familiarizing yourself with the different use cases of curl, you can effectively use it for tasks like downloading files, sending data, and authenticating with servers.

Related Posts

Drupal-Check: Drupal PHP Code Deprecation Checker (with examples)

Drupal-Check: Drupal PHP Code Deprecation Checker (with examples)

Introduction Drupal is a popular open-source content management system (CMS) that is widely used to build websites and applications.

Read More
How to use the command 'adb install' (with examples)

How to use the command 'adb install' (with examples)

The ‘adb install’ command is part of the Android Debug Bridge (ADB) tool and is used to push packages, specifically Android application files (APK), to an Android emulator instance or a connected Android device.

Read More
How to Use the Command 'fatlabel' (with examples)

How to Use the Command 'fatlabel' (with examples)

The ‘fatlabel’ command is used to set or get the label of a FAT32 partition.

Read More