# cURLing Your Brain

Have you ever wondered how powerful a simple black window with a blinking cursor can be? To the uninitiated, it looks like a scene from a 90s hacker movie. But to a developer, that cursor is a gateway.  
Whenever you search “google.com”, the browser sends a **request** to the server, and receives a **response** that contains all the HTML, CSS and JS to display the website in your browser.

Your browser is just one way to talk to servers. But what if you want to talk to servers without opening a browser? What if you're building an application that needs to fetch data from another service? This is where **cURL** comes in.

## What is cURL?

**cURL: Client URL** is a command-line tool, can help you to access web data, work with APIs, and other such tasks with only command-line. It helps the client to communicate with the server using text.

### Why Do Developers Need cURL?

Here's why cURL is essential for developers:

1. **Testing APIs Without Writing Code:** When building or using an API (Application Programming Interface), it can be tested if it works correctly. cURL send test requests without writing a full application first.
    
2. **Debugging:** When something goes wrong with a web application, cURL helps to see exactly what's being sent to the server and what's coming back.
    
3. **Automation and Scripting:** It can be used in scripts to automate tasks like downloading files, checking if a website is up, or fetching data from APIs on a schedule.
    
4. **Understanding How the Web Works:** cURL helps to gain an understanding of how the web works when you click a link or submit a form.
    

## Making Your First Request Using cURL

Most operating systems ships cURL by default, you can check if it is installed by typing `curl --version` in your terminal. If it is unavailable, follow the installation guide ([https://curl.se/download.html](https://curl.se/download.html)) to install.

Now, fire up your terminal for your first command:

```bash
curl https://example.com
```

The moment you press enter, following happens

* cURL sends a **GET** request to example.com
    
* The server at example.com receives your request
    
* The server responds with HTML content of the homepage
    
* cURL displays HTML content in your terminal
    

<iframe src="https://app.warp.dev/block/embed/U8PQNQZcZfQOXI2dnP4oqn" style="width:1262px;height:283px;border:0;overflow:hidden"></iframe>

If you are interested in `headers` of the webpage:

```bash
#Displays headers and HTML content
curl -i https://example.com 

#Displays only headers
curl -I https://example.com
```

The `-i` or `-I` flag tells cURL to include the response headers. These **headers** contain metadata about the response.

<iframe src="https://app.warp.dev/block/embed/9yXrnGDEhhC0z0Mp8lBUpw" style="width:1262px;height:416px;border:0;overflow:hidden"></iframe>

The interaction between the two looks like:

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1769771043823/83d1938f-0009-434a-a0d3-dfff247284ad.png align="center")

## cURL for HTTP Methods

cURL is like “waiters” of the internet that pass data between apps. It is a very powerful tool to communicate to APIs.

APIs use JSON to send and receive data. Learn more about JSON and RESTful API [here](https://jsonplaceholder.typicode.com/guide/).

### GET- Getting Data

GET can fetch data or resources from the server.

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

The JSON data retrieved looks like:

<iframe src="https://app.warp.dev/block/embed/NEuIBGwaItNdCxaOaQcbUT" style="width:1262px;height:625px;border:0;overflow:hidden"></iframe>

### POST- Creating Request

GET requests ask for data. **POST requests send data** to the server like submission of the form or make a new entry in the database.

```bash
curl -X POST https://jsonplaceholder.typicode.com/posts \
  -H "Content-Type: application/json" \
  -d '{"title": "My First Post", "body": "This is the content", "userId": 1}'
```

In the command:

* `-X POST`: Specifies to use POST Method
    
* `-H "Content-Type: application/json"`: sets the HTTP header to indicate JSON data is being sent
    
* `-d '...'`: The data being send
    

<iframe src="https://app.warp.dev/block/embed/sQ4uaKm1NW9gr7PUCju9LZ" style="width:1262px;height:340px;border:0;overflow:hidden"></iframe>

### **PUT & PATCH - Updating the Resource**

To update the resources, PUT and PATCH is used.

* `PUT`: Replace the entire source
    
* `PATCH`: Modify the data
    

```bash
curl -X PUT https://jsonplaceholder.typicode.com/posts/1 \
  -H "Content-Type: application/json" \
  -d '{"title": "Test", "body": "Testing cURL", "userId": 1}'
```

In the command:

* `-X PUT`: specifies PUT request.
    
* [`https://jsonplaceholder.typicode.com/posts/1`](https://jsonplaceholder.typicode.com/posts/1) is the URL of the post to be updated.
    
* `-H "Content-Type: application/json"`: sets the content type to JSON.
    
* `-d '...'` : the JSON data with the desired changes.
    

<iframe src="https://app.warp.dev/block/embed/jB1VvbjQtwtlDP5KT9tgbT" style="width:1262px;height:340px;border:0;overflow:hidden"></iframe>

In case of modification of data:

```bash
curl -X PATCH https://jsonplaceholder.typicode.com/posts/1 \
  -H "Content-Type: application/json" \
  -d '{"title": "Test", "body": "Testing cURL", "userId": 1}'
```

### **DELETE - Deleting the Resource**

DELETE is used to remove the data or record from the server or database.

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

<iframe src="https://app.warp.dev/block/embed/NqMintUxvTj4eYJQvVb7FC" style="width:1262px;height:226px;border:0;overflow:hidden"></iframe>

**Try It Yourself:** Fire up the terminal and try a few bash commands yourself.

* Fetch a website using `curl` [`https://example.com`](https://example.com)
    
* GET user data from API using `curl` [`https://jsonplaceholder.typicode.com/users`](https://jsonplaceholder.typicode.com/users)
    
* Get a specific user using `curl` [`https://jsonplaceholder.typicode.com/users/1`](https://jsonplaceholder.typicode.com/users/1)
    
* Create POST request
    
* Save the response to a file using `curl` [`https://jsonplaceholder.typicode.com/users`](https://jsonplaceholder.typicode.com/users) `> users.json`
    

Once you get comfortable with methods, authentication, file uploads, custom headers, and debugging network issues. The simplicity of cURL makes it a very powerful and valuable tool for developers.  
PS: Please learn cURL before you touch Postman (This includes Apidog). Happy cURLing !
