# Getting Started with cURL

Before we talk about what cURL or curl lets discuss what a **server** is\*\*,\*\* and why we need it

In simple terms, a **server** is a computer or system that provides data, services, or programs to **clients**. Servers *receive* requests from clients to retrieve files or data stored (e.g., in a database). Like your computer, a server has an operating system—such as Ubuntu, Windows, etc.

### **Types of Servers**

* **Web Servers** — Deliver web content (HTML, CSS, JS, images).
    
* **File Servers** — Store and manage files.
    
* **Mail Servers** — Handle incoming and outgoing emails.
    
* **Database Servers** — Manage databases and applications.
    

### Types of Servers

* Web Servers - Deliver web contents
    
* File Servers - Store and Manage files
    
* Mail Servers - Handles incoming & outgoing mails
    
* Database Servers - Manage databases and applications.
    

## **What is cURL? (In Very Simple Terms)**

**cURL** is an open-source **command-line tool and library** that lets you transfer data using various network protocols (HTTP, HTTPS, FTP, and more). It’s often used to:

* Interact with **APIs**
    
* **Download** files
    
* **Test** how a server responds
    

**cURL** stands for **Client URL** — you (the client) use it to talk to a URL (a server).

### **How cURL Sends Messages to a Server**

cURL builds an **HTTP request** and sends it to a specific **URL**. It handles the full request–response cycle in the terminal—no browser or UI needed.

```bash
curl https://shivam-goyal.site
```

**Key Components of a cURL Message**

1. **URL:** The destination server address (e.g., `https://shivam-goyal.site`).
    
2. **HTTP Method (**`-X`): Defines the action (e.g., `GET`, `POST`, `PUT`, `DELETE`). If not specified, it defaults to `GET`.
    
3. **Headers (**`-H`): Metadata about the request, such as Content-Type (e.g., `-H "Content-Type: application/json"`).
    
4. **Body (**`-d`): The message data itself. Used with `POST` our `PUT` methods.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769576551015/e12427fe-0d77-44a9-8cbf-89af66489ef7.png align="center")

**What just happened?**

1\. cURL sent a **GET** request to [`https://shivam-goyal.site`](https://shivam-goyal.site).

2\. The server at that URL processed the request.

3\. The server sent back a **response** (status + headers + body).

4\. cURL printed the **body** (the HTML) to your terminal.

## **Understanding Request and Response**

Every HTTP exchange has two parts: **request** (what you send) and **response** (what the server sends back).

### **The Response**

When you run `curl` [`https://shivam-goyal.site`](https://shivam-goyal.site), you mainly see the **body**. But the full response also includes:

1\. **Status code** — A number that tells you if the request succeeded or failed.

* **200** — OK (success).
    
* **404** — Not Found.
    
* **500** — Server error.
    

2\. **Headers** — Metadata (content type, date, etc.).

3\. **Body** — The actual data (HTML, JSON, etc.).

To see **status and headers** as well, use `-i` (include headers):

```bash
curl -i https://shivam-goyal.site
```

You’ll see something like:

```bash
HTTP/2 200

content-type: text/html; charset=UTF-8
```

Then the HTML follows. **200** means the request succeeded.

## **GET vs POST (Only These Two for Now)**

* **GET** — “Give me data.” Used to **read** or **fetch** something. Nobody needed. Default in cURL.
    
* Example: open a URL, fetch a user, list items.
    

**POST** — “Here is some data; do something with it.” Used to **send** data to the server (e.g., create a user, submit a form).

* You often send a **body** (e.g., JSON) and set **Content-Type**.
    

### **GET (default)**

```bash
curl https://api.shivam-goyak.site/users
```

Same as:

```bash
curl -X GET https://api.shivam-goyal.site/users
```

### **POST (sending data)**

```bash
curl -X POST https://api.shivam-goyal.site/users \

  -H "Content-Type: application/json" \

  -d '{"name":"Alice","email":"alice@example.com"}'
```

```bash
- -X POST — use POST method.

- -H "Content-Type: application/json" — body is JSON.

- -d '...' — the JSON body.
```

Stick to **GET** and **POST** at first; add PUT/DELETE later when you’re comfortable.

## **Using cURL to Talk to APIs**

Many services expose **APIs**—URLs that return or accept data (often JSON) instead of a full webpage.

1\. **Read the API docs** — They tell you the URL, method (GET/POST), and whether you need headers or a body.

2\. **Start with GET** — Try fetching a public API:

```bash
   curl https://jsonplaceholder.typicode.com/posts/1
```

   You’ll get JSON for “post” with id 1.

3\. **Then try POST** — Same site allows a sample POST:

```bash
curl -X POST https://jsonplaceholder.typicode.com/posts \
     -H "Content-Type: application/json" \
     -d '{"title":"Hello","body":"My first post","userId":1}'
```

That’s the core idea: **same tool (cURL), different URLs and methods** — you’re “talking” to the API from the terminal.

## **Common Mistakes Beginners Make with cURL**

1\. **Forgetting quotes** — If the URL or JSON has `&`, `?`, or spaces, put it in quotes:

```bash
 curl "https://example.com?name=John Doe"
```

2\. **Wrong Content-Type for POST** — If you send JSON, set the header:

```bash
  -H "Content-Type: application/json"
```

3\. **Typos in the URL** — `http` vs `https`, missing slash, wrong domain. Double-check the URL.

4\. **Using GET when you need POST** — Creating or updating data usually needs POST (or PUT). Check the API docs.

5\. **Ignoring the status code** — Always look at the first line of the response (e.g., `HTTP/2 200` or `404`). Use `curl -i` to see it.

Start with simple commands; add flags only when you need them.

## **Where cURL Fits in Backend Development**

* **Backend dev** — You build the server. cURL lets you call your own APIs (GET/POST) without a frontend.
    
* **Testing** — Quick checks: “Does this endpoint return 200? What JSON do I get?”
    
* **Scripts** — Cron jobs, deploy scripts, or automation that hit a URL.
    
* **Learning** — You see the raw HTTP request and response, which helps when you use libraries (e.g., `fetch`, Axios) in code.
    

Conceptually, **Browser** and **cURL** both send HTTP requests; the browser also renders HTML and runs JS. cURL just shows you the “wire” — the request and response — which is why it’s so useful for APIs and debugging.
