Post

cURL Command Arsenal

cURL Command Arsenal

In the world of API testing, network debugging, and data transfers, the humble cURL command stands as one of the most powerful tools in a developer’s arsenal. This versatile command-line utility lets you send requests, transfer data, and interact with web services with remarkable precision. In this comprehensive guide, we’ll explore the most useful authentication and request options that will transform you from a cURL novice to a command-line virtuoso.

The cURL Fundamentals

Before diving into advanced options, let’s establish a baseline. At its core, cURL (Client URL) is designed to transfer data to or from a server using various protocols, with HTTP being the most common.

The basic syntax is refreshingly simple:

1
curl https://example.com

This command performs a GET request to the specified URL and outputs the response to your terminal. But cURL’s true power lies in its extensive options that allow for precise control over your requests.

Authentication Methods

Basic Authentication

When accessing protected resources, basic authentication is often your first line of approach:

1
curl -u username:password https://api.example.com/resource

For better security, omit the password from the command line to trigger a password prompt:

1
curl -u username https://api.example.com/resource

Bearer Token Authentication

For modern APIs using OAuth or JWT:

1
curl -H "Authorization: Bearer YOUR_TOKEN_HERE" https://api.example.com/resource

API Key Authentication

Many services use API keys, typically passed as a header or query parameter:

1
2
3
4
5
# As a header
curl -H "X-API-Key: YOUR_API_KEY" https://api.example.com/resource

# As a query parameter - not safest
curl "https://api.example.com/resource?api_key=YOUR_API_KEY"

OAuth 2.0 Client Credentials

For obtaining access tokens with client credentials:

1
2
curl -X POST https://auth.example.com/token \
  -d "grant_type=client_credentials&client_id=YOUR_CLIENT_ID&client_secret=YOUR_CLIENT_SECRET"

Request Methods and Data

HTTP Methods

cURL defaults to GET, but you can specify any HTTP method:

1
2
3
4
curl -X POST https://api.example.com/create
curl -X PUT https://api.example.com/update/123
curl -X DELETE https://api.example.com/delete/123
curl -X PATCH https://api.example.com/partial-update/123

Sending Form Data

For traditional form submissions:

1
2
curl -X POST https://api.example.com/submit \
  -d "name=John&[email protected]"

JSON Payloads

For RESTful APIs expecting JSON:

1
2
3
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name": "John Doe", "email": "[email protected]"}'

For larger JSON payloads, you can reference a file:

1
2
3
curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d @user-data.json

Headers and Cookies

Custom Headers

Headers provide crucial metadata for your requests:

1
2
3
4
curl -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -H "User-Agent: My-App/1.0" \
  https://api.example.com/resource

Working with Cookies

For services that use cookies for session management:

1
2
3
4
5
# Save cookies to a file
curl -c cookies.txt https://example.com/login

# Send cookies from file
curl -b cookies.txt https://example.com/dashboard

Advanced Request Options

Following Redirects

By default, cURL doesn’t follow redirects. Enable this with:

1
curl -L https://example.com/redirecting-url

Setting Timeouts

Prevent hanging requests with timeouts:

1
2
# Connection timeout of 5 seconds, maximum time of 30 seconds
curl --connect-timeout 5 --max-time 30 https://api.example.com/resource

Controlling Output

Silence progress meters and errors for clean output:

1
2
3
curl -s https://api.example.com/data     # Silent mode
curl -S https://api.example.com/data     # Show errors
curl -s -S https://api.example.com/data  # Silent but show errors

Save the response to a file instead of displaying it:

1
curl -o response.json https://api.example.com/data

Debugging Requests

When troubleshooting, verbose mode is invaluable:

1
curl -v https://api.example.com/resource

For even more detailed information:

1
curl --trace output.txt https://api.example.com/resource

To see just the response headers:

1
curl -I https://api.example.com/resource

Certificate Handling

Bypassing Certificate Verification

Warning: Use only in development or trusted environments!

1
curl -k https://self-signed.example.com

Specifying Certificates

For mutual TLS authentication:

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

Proxies and Networking

Using Proxies

Route requests through proxies:

1
curl -x http://proxy.example.com:8080 https://api.example.com/resource

For authenticated proxies:

1
curl -x http://user:[email protected]:8080 https://api.example.com/resource

Controlling Network Interface

Specify which network interface to use:

1
curl --interface eth0 https://api.example.com/resource

Performance Testing

Multiple Requests

For basic load testing:

1
for i in {1..100}; do curl -s https://api.example.com/endpoint & done

Download Speed Testing

Measure download speeds:

1
curl -o /dev/null -s -w "Speed: %{speed_download} bytes/sec\n" https://example.com/large-file

Putting It All Together

Here’s an example combining multiple options for a complex API request:

1
2
3
4
5
6
7
8
9
10
curl -X POST https://api.example.com/orders \
  -H "Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..." \
  -H "Content-Type: application/json" \
  -H "Accept: application/json" \
  -d '{"product_id": 123, "quantity": 2}' \
  -s -S \
  -o response.json \
  --connect-timeout 5 \
  --max-time 30 \
  -w "\nStatus: %{http_code}\nTime: %{time_total}s\n"

Conclusion

cURL’s flexibility and power make it an indispensable tool for developers, DevOps engineers, and system administrators. By mastering its authentication and request options, you gain the ability to interact with virtually any web service or API directly from your terminal.

Whether you’re debugging API interactions, automating tasks, or testing services, this cURL command arsenal equips you with the techniques to handle even the most complex scenarios. Keep this guide handy as a reference, and you’ll find yourself reaching for cURL as your go-to tool for HTTP interactions.

Remember that while cURL is powerful on its own, it can also serve as a stepping stone to understanding how to implement these same interactions in your programming language of choice, as the concepts of headers, authentication, and request parameters remain consistent across different HTTP clients.

This post is licensed under CC BY 4.0 by the author.