Mastering cURL Command: Advanced Techniques for HTTP Requests and Data Transfer

A-digital-illustration-featuring-a-variety-of-networking-tools-all-labeled
02/06/2024 •

How to use cURL Command?

The curl command is a versatile tool for making HTTP requests and interacting with various protocols. Here’s a basic overview of how to use the curl command with some common options:

cURL Command

1. Making a Simple GET Request

To make a simple GET request to a URL, use the following syntax:

    curl [URL]
  

For example: cURL Command

     curl https://www.example.com 
  

This command will display the HTML content of the specified webpage in the terminal.

2. Specifying HTTP Method

You can specify the HTTP method using the -X option. For example, to make a POST request:

     curl -X POST [URL] 
  




3. Sending Data with POST Request

To send data with a POST request, you can use the -d option followed by the data you want to send:

     curl -X POST -d "key1=value1&key2=value2" [URL] 
  

4. Adding Headers

You can include custom headers using the -H option:

     curl -H "Content-Type: application/json" [URL] 
  




5. Following Redirects

To automatically follow redirects, use the -L option:

     curl -L [URL] 
  




6. Saving Output to a File

You can save the output to a file using the -o option:

     curl -o output.html https://www.example.com 
  




7. Displaying HTTP Response Headers

To display the HTTP response headers along with the content, use the -i option:

     curl -i https://www.example.com 
  

8. Handling Cookies

To handle cookies, use the -b option to send cookies and the -c option to save cookies to a file:

     curl -b "cookie1=value1; cookie2=value2" -c cookies.txt [URL] 
  




9. Insecure SSL Connections

If you are making requests to a server with a self-signed SSL certificate, you can use the -k or --insecure option to ignore SSL verification:

     curl -k [URL] 
  




10. Displaying Progress

To display a progress bar during the transfer, use the -# option:

     curl -# [URL] 
  




Example with Multiple Options

     curl -X POST -H "Content-Type: application/json" -d '{"key": "value"}' https://api.example.com 
  

This command makes a POST request to the specified API endpoint with a JSON payload and a custom header.

The curl command is a powerful tool with numerous options for making HTTP requests and interacting with various protocols. Here’s an overview of some common curl command options:

Basic Options

  1. URL:
    • curl [URL]: Makes a GET request to the specified URL.
  2. HTTP Methods:
    • -X, --request [HTTP_METHOD]: Specifies the HTTP method (GET, POST, PUT, DELETE, etc.).

Data Transfer

  1. Data Sending:
    • -d, --data [DATA]: Sends data in a POST request.
    • --data-urlencode [DATA]: URL-encodes data for a POST request.
  2. Form Submission:
    • -F, --form [KEY=VALUE]: Sends data as a form in a multipart/form-data POST request.
  3. File Upload:
    • --upload-file [FILE]: Uploads a file using a PUT request.
  4. Follow Redirects:
    • -L, --location: Follows HTTP redirects.

Headers:

  1. Custom Headers:
    • -H, --header [HEADER]: Adds custom headers to the request.

Output Control

  1. Save to File:
    • -o, --output [FILE]: Saves output to a file.
  2. Display Headers:
    • -i, --include: Includes the HTTP headers in the output.

SSL/TLS

  1. Ignore SSL Certificate Verification:
    • -k, --insecure: Allows connections to SSL sites without certificates.

Authentication

  1. Basic Authentication:
    • -u, --user [USERNAME:PASSWORD]: Specifies the username and password for basic authentication.
  2. API Key Authentication:
    • --header "API-Key: [KEY]": Sends an API key as a header.

Cookies

  1. Send Cookies:
    • -b, --cookie [COOKIE]: Sends cookies in the request.
  2. Save Cookies:
    • -c, --cookie-jar [FILE]: Saves cookies to a file.

Progress

  1. Progress Bar:
    • -#: Displays a progress bar during transfers.

Other

  1. User Agent:
    • -A, --user-agent [AGENT]: Specifies the User-Agent string.
  2. Timeout:
    • --connect-timeout [SECONDS]: Sets the maximum time allowed for the connection.
  3. Verbose Output:
    • -v, --verbose: Provides more detailed output for debugging.

Example Usage

# Basic GET request

     curl https://www.example.com 
  

# POST request with data

     curl -X POST -d "key1=value1&key2=value2" https://api.example.com 
  

# Sending JSON data

     curl -X POST -H "Content-Type: application/json" -d '{"key": "value"}' https://api.example.com 
  

https://curl.se

https://sanchitgurukul.com/basic-networking

https://sanchitgurukul.com/network-security

Disclaimer: This article may contain information that was accurate at the time of writing but could be outdated now. Please verify details with the latest vendor advisories or contact us at admin@sanchitgurukul.com.

Discover more from

Subscribe now to keep reading and get access to the full archive.

Continue reading