Skip to main content

Command Palette

Search for a command to run...

Getting Started with cURL: Your Complete Guide

Updated
β€’9 min read
A

A MERN stack web developer blending computer science fundamentals with AI to build practical, real‑world solutions. With a background in Mathematics, Physics, and an MCA, I enjoy creating interactive web applications, quiz platforms, and study tools that simplify complex topics. I focus on writing clean code, learning through real projects, and using AI tools to move from idea to deployment faster.

Introduction

Imagine you want to send a message to someone. You write it down, put it in an envelope, and drop it in a mailbox. The mailbox is like a server - it receives messages from you and can send you responses back. Just like servers on the internet!

Every time you visit a website, your browser is sending a message to a server asking for the website's content. The server receives this message, processes it, and sends back the data your browser displays.

cURL is a powerful tool that lets you send these messages directly from your computer's terminal (command line), without needing a web browser. In this guide, we'll explore what cURL is, why developers love it, and how you can start using it today.

πŸ“Š How Client-Server Communication Works

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”          β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚   Your       β”‚          β”‚   Internet   β”‚          β”‚    Server    β”‚
β”‚   Computer   │◄────────►│   Network    │◄────────►│  (Website)   β”‚
β”‚   (Client)   β”‚          β”‚              β”‚          β”‚              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜          β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
      β–²                                                     β–²
      β”‚                                                     β”‚
      β”‚ 1. Send Request: "Show me example.com"             β”‚
      β”‚                                                     β”‚
      β”‚ 2. Server Response: "Here's the HTML code!"        β”‚
      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Whether you use a browser or cURL, this is the same basic process!


What is cURL? (In Very Simple Terms)

cURL stands for Client URL. But forget the technical name - think of it this way:

cURL is a messenger that talks to servers on the internet from your terminal.

Just like your web browser sends requests to servers when you click on websites, cURL does the same thing - but through text commands. It's incredibly lightweight, fast, and doesn't need a graphical interface.

When you use cURL, you:

  1. Type a command in your terminal

  2. Send a message to a server

  3. Get back a response

  4. See the response in your terminal

That's it! No browser needed.

Why Not Use a Browser?

Your browser is excellent for viewing websites, but sometimes you need something more:

  • Test an API without opening a browser

  • Automate requests in scripts

  • Debug network issues

  • Work on a server without a graphical interface

  • Send files or data quickly

cURL does all of this and more.

πŸ†š Browser vs cURL Comparison

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚    🌐 BROWSER             β”‚     β”‚     πŸ“¦ cURL                β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ βœ… Visual interface       β”‚     β”‚ βœ… Command-line only      β”‚
β”‚ βœ… Renders HTML/CSS        β”‚     β”‚ βœ… Raw data output        β”‚
β”‚ βœ… Runs JavaScript         β”‚     β”‚ βœ… Fast & lightweight     β”‚
β”‚ ❌ Harder to automate      β”‚     β”‚ βœ… Easy to script         β”‚
β”‚ ❌ Needs GUI               β”‚     β”‚ βœ… Works on servers       β”‚
β”‚ ❌ Heavier resource usage  β”‚     β”‚ βœ… API testing friendly   β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

Both are powerful tools - they just serve different purposes!


Why Programmers Need cURL

Programmers use cURL almost daily. Here's why:

1. API Testing

When you're building web applications, you often need to communicate with APIs (Application Programming Interfaces). cURL lets you test these APIs instantly from the command line without writing code.

2. Automation

You can include cURL commands in scripts that run automatically. For example, fetching data from an API every hour or downloading files.

3. Debugging

When something goes wrong with your web application, cURL helps you debug network issues by showing you exactly what data is being sent and received.

4. Server-side Operations

If you're working on a server without a graphical interface, you can still communicate with APIs and retrieve data using cURL.

5. It's Everywhere

cURL is available on almost every operating system - Windows, Mac, Linux. Once you learn it, you can use it anywhere.

6. No Extra Tools Needed

You don't need to install special software. cURL comes pre-installed on most systems, or you can install it easily.


Making Your First Request Using cURL

Let's get hands-on! Open your terminal (Command Prompt on Windows, Terminal on Mac/Linux) and type this command:

curl https://www.example.com

Then press Enter.

What Just Happened?

You just sent a request to the example.com server asking for its webpage. The server sent back HTML code (the code that creates webpages), and cURL displayed it in your terminal.

That's literally all cURL does - it sends a message to a server and shows you the response!

Your First API Request

Want to try something cooler? Here's a request to a real API that returns JSON data:

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

Run this and you'll get information about the GitHub user in JSON format (JSON is a way to structure data).

See? You're already using cURL like a pro!


Understanding Request and Response

Let's break down what happens when you run a cURL command:

The Request Flow

Your Terminal
     ↓
  cURL
     ↓
Internet
     ↓
  Server

You type a cURL command β†’ cURL packages your request β†’ Sends it over the internet β†’ Server receives it β†’ Server sends back a response β†’ You see it in your terminal.

What Makes Up a Request?

Every cURL request has:

  1. URL - Where you're sending the request

  2. Method - What action you want (more on this below)

  3. Headers - Extra information about the request

  4. Body - Optional data you're sending

πŸ“¦ HTTP Request Structure

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              HTTP REQUEST                           β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1️⃣ METHOD: GET                                    β”‚
β”‚    πŸ‘‰ What action? (GET, POST, PUT, DELETE, etc.) β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 2️⃣ URL: https://api.example.com/users/123       β”‚
β”‚    πŸ‘‰ Where to send the request?                  β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 3️⃣ HEADERS:                                       β”‚
β”‚    Content-Type: application/json               β”‚
β”‚    Authorization: Bearer YOUR_TOKEN             β”‚
β”‚    πŸ‘‰ Extra metadata about the request            β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 4️⃣ BODY (optional):                               β”‚
β”‚    {                                            β”‚
β”‚      "name": "John Doe",                          β”‚
β”‚      "email": "john@example.com"                 β”‚
β”‚    }                                            β”‚
β”‚    πŸ‘‰ Data you're sending (for POST/PUT)          β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

The Response

Every response has:

  1. Status Code - A number like 200 (success), 404 (not found), 500 (server error)

  2. Headers - Information about the response

  3. Body - The actual data (HTML, JSON, etc.)

πŸ“₯ HTTP Response Structure

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚              HTTP RESPONSE                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 1️⃣ STATUS CODE: 200 OK                           β”‚
β”‚    πŸ‘‰ 200 = Success                               β”‚
β”‚    πŸ‘‰ 404 = Not Found                             β”‚
β”‚    πŸ‘‰ 500 = Server Error                          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 2️⃣ HEADERS:                                       β”‚
β”‚    Content-Type: application/json               β”‚
β”‚    Content-Length: 142                          β”‚
β”‚    Server: nginx/1.18.0                         β”‚
β”‚    πŸ‘‰ Metadata about the response                 β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 3️⃣ BODY:                                          β”‚
β”‚    {                                            β”‚
β”‚      "id": 123,                                   β”‚
β”‚      "name": "John Doe",                          β”‚
β”‚      "email": "john@example.com"                 β”‚
β”‚    }                                            β”‚
β”‚    πŸ‘‰ The actual data you requested              β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

GET vs POST (The Two Methods You Need to Know)

GET Request:

  • Used to retrieve data

  • Like asking a question

  • Data goes in the URL

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

POST Request:

  • Used to send data

  • Like submitting a form

  • Data goes in the body



curl -X POST https://api.example.com/users -d "name=John&age=30"

βš”οΈ GET vs POST: Visual Comparison

β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”      β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”‚        πŸ”½ GET METHOD          β”‚      β”‚        πŸ“ POST METHOD         β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ 🎯 Purpose                   β”‚      β”‚ 🎯 Purpose                   β”‚
β”‚ Retrieve/Read data         β”‚      β”‚ Send/Create/Update data    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ πŸ“€ Data Location             β”‚      β”‚ πŸ“€ Data Location             β”‚
β”‚ In the URL                 β”‚      β”‚ In the request BODY        β”‚
β”‚ (visible in address bar)   β”‚      β”‚ (hidden from URL)          β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ πŸ” Security                   β”‚      β”‚ πŸ” Security                   β”‚
β”‚ Less secure (visible)      β”‚      β”‚ More secure (hidden)       β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ πŸ“¦ Example                   β”‚      β”‚ πŸ“¦ Example                   β”‚
β”‚ curl example.com/api?id=5  β”‚      β”‚ curl -X POST example.com   β”‚
β”‚                            β”‚      β”‚      -d "id=5&name=John"    β”‚
β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€      β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚ βœ… Can be bookmarked          β”‚      β”‚ ❌ Cannot be bookmarked       β”‚
β”‚ βœ… Can be cached              β”‚      β”‚ ❌ Not cached                 β”‚
β”‚ ❌ Limited data size          β”‚      β”‚ βœ… Can send large data        β”‚
β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜      β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜

That's all you need to know about GET

and POST for now. (There are other methods like PUT, DELETE, etc., but we'll skip them for this beginner guide.)


Using cURL to Talk to APIs

Now let's put it all together and interact with a real API.

Example 1: Fetch Random User Data

Try this command:

curl https://randomuser.me/api/

You'll get JSON data with random user information. Cool, right?

Example 2: Send Data to an API

Here's how to send data with a POST request:

curl -X POST https://httpbin.org/post -d "message=Hello World"

The -X POST tells cURL you're making a POST request. The -d flag contains the data you're sending.

Example 3: Add Headers to Your Request

Sometimes APIs need extra information:

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

The -H flag lets you add headers.

Example 4: Save Response to a File

Instead of displaying the response in the terminal, save it:

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

The -o flag saves the response to a file.


Common Mistakes Beginners Make with cURL

1. Forgetting the Full URL

❌ Wrong:

curl example.com

βœ… Right:

curl https://example.com

Always include https:// or http://

2. Using the Wrong Method

If an API expects POST but you send GET, it won't work.

❌ Wrong:

curl https://api.example.com/submit-form

βœ… Right:

curl -X POST https://api.example.com/submit-form

3. Not Including Required Headers

Some APIs require authentication tokens or content-type headers.

❌ Wrong:

curl -X POST https://api.example.com/data

βœ… Right:

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

4. Forgetting to Escape Quotes

When sending JSON data, you need to be careful with quotes.

❌ Wrong:

curl -d "{"name": "John"}"

βœ… Right:

curl -d '{"name": "John"}'

Or use escaping:

curl -d "{\"name\": \"John\"}"

5. Ignoring the Response

Always look at what the server is telling you. If something fails, the response often explains why.

6. Not Using the Verbose Flag When Debugging

When things go wrong, use:

curl -v https://api.example.com/data

The -v flag shows you everything - headers, request details, and the full response. Super helpful!


Quick Reference: Essential cURL Flags

Here are the most important flags for beginners:

FlagPurposeExample
-XSet HTTP methodcurl -X POST url
-dSend datacurl -d "param=value" url
-HAdd headercurl -H "Content-Type: application/json" url
-oSave to filecurl -o file.html url
-vVerbose (see everything)curl -v url
-LFollow redirectscurl -L url

Key Takeaways

βœ… cURL is a tool to send messages to servers from your terminal

βœ… Perfect for testing APIs and automating tasks

βœ… GET is for retrieving data, POST is for sending data

βœ… Always include the full URL with https://

βœ… Use the -v flag to debug problems

βœ… Experiment! The best way to learn cURL is by trying it


What's Next?

Now that you understand cURL basics:

  • Try it with different APIs

  • Learn about authentication tokens

  • Explore more advanced flags

  • Use it in your scripts and automation

Remember: every programmer started as a beginner. You've got this! πŸš€


Resources

Happy curling!