GeekDas
3 min read
GeekdasGeekdas

cURL Command Guide: Usage, Examples, and Common Mistakes

cURL Command Guide: Usage, Examples, and Common Mistakes

If you work with APIs, servers, or websites, you’ve likely heard of cURL. Short for Client URL, curl is a powerful command-line tool used to transfer data between systems using URLs. From testing APIs to downloading files, sending POST requests, handling authentication, and debugging HTTP issues-cURL is a must-know utility for developers, DevOps engineers, and system administrators.

In this complete guide, you’ll learn how to set up cURL, run practical commands, avoid common mistakes, and understand how it compares to other tools in the same category.

What is cURL?

cURL is an open-source command-line tool and library for transferring data with URLs. It supports numerous protocols, including:

  • HTTP / HTTPS

  • FTP / FTPS

  • SFTP / SCP

  • SMTP / IMAP / POP3

  • LDAP and more

It’s widely used for:

  • API testing

  • Debugging server responses

  • Automating downloads

  • Authentication testing

  • Sending form data

Setting Up cURL

Most systems already include cURL.

Check if cURL is installed

curl --version

Install cURL

macOS (via Homebrew)

brew install curl

Ubuntu / Debian Linux

sudo apt install curl

Windows

Modern Windows versions include cURL in Command Prompt and PowerShell.

Basic cURL Syntax

curl [options] [URL]

Example:

curl https://example.com

Practical cURL Command Examples

Making a Simple GET Request

curl https://api.example.com/users

This fetches data from the URL using the default GET method.

Sending a POST Request with Data

curl -X POST https://api.example.com/users \
  -H "Content-Type: application/json" \
  -d '{"name":"John","email":"john@example.com"}'

Adding Headers to a Request

curl -H "Authorization: Bearer TOKEN" https://api.example.com/profile

Saving Output to a File

curl -o file.html https://example.com

Following Redirects

curl -L http://example.com

Viewing Response Headers

curl -I https://example.com

Uploading a File

curl -F "file=@document.pdf" https://example.com/upload

Debugging with Verbose Mode

curl -v https://example.com

Using cURL for API Testing

cURL is heavily used to test REST APIs. You can simulate requests exactly like a frontend or mobile app would send.

curl -X PUT https://api.example.com/users/1 \
  -H "Authorization: Bearer TOKEN" \
  -d '{"role":"admin"}'

Authentication with cURL

Basic Auth

curl -u username:password https://example.com

Bearer Token

curl -H "Authorization: Bearer TOKEN" https://api.example.com

Working with Query Parameters

curl "https://api.example.com/search?q=curl&limit=10"

Common Mistakes When Using cURL

1. Not specifying the protocol

Wrong:

curl example.com

Correct:

curl https://example.com

2. Forgetting quotes around URLs with parameters

Wrong:

curl https://api.com?q=hello world

Correct:

curl "https://api.com?q=hello world"

3. Missing -L for redirects

Some sites redirect HTTP HTTPS.

4. Wrong content type for POST requests

Always set:

-H "Content-Type: application/json"

5. Not using verbose mode while debugging

Use -v to see request/response details.

Benefits of Using cURL

  • Available on almost every OS

  • Lightweight and fast

  • Supports many protocols

  • Perfect for automation and scripting

  • Excellent for API debugging

  • No GUI needed

Comparison: cURL vs Other Tools

Feature

cURL

Postman

wget

Interface

CLI

GUI

CLI

API Testing

Excellent

Excellent

Limited

Automation

Excellent

Limited

Good

Learning Curve

Medium

Easy

Easy

Protocol Support

Very High

HTTP focused

HTTP/FTP

Scripting Friendly

Yes

No

Yes

When to use cURL: automation, servers, CI/CD, debugging.
When to use Postman: visual API exploration.
When to use wget: simple file downloads.

Conclusion

cURL is one of the most essential tools in a developer’s toolkit. Whether you’re testing APIs, debugging network issues, uploading files, or automating data transfers, mastering cURL saves time and gives you precise control over HTTP requests. With the examples and best practices in this guide, you can confidently use cURL in real-world scenarios and avoid common pitfalls.

Frequently Asked Questions

What is cURL used for?
cURL is used to transfer data using URLs, commonly for API testing, downloading files, and debugging HTTP requests.
Is cURL better than Postman?
cURL is better for automation and scripting, while Postman is better for visual API testing.
How do I install cURL on Windows?
Modern Windows includes cURL by default. You can verify with curl --version.
How do I send JSON data with cURL?
Use -X POST, -H "Content-Type: application/json", and -d '{json}'.
Why is my cURL request getting redirected?
Because the server redirects HTTP to HTTPS. Use -L to follow redirects.
Can cURL handle authentication?
Yes. It supports Basic Auth, Bearer Tokens, OAuth, and more.
How do I see headers in a cURL response?
Use the -I option to view only headers.
Is cURL only for developers?
Primarily yes, but system admins and DevOps engineers also rely on it heavily.

Related Articles