Skip to main content

Command Palette

Search for a command to run...

Getting Started with cURL

Published
4 min read
A

MERN Stack Developer

Understanding Servers First

Before we talk about cURL, let's understand what a server is.

Think of a server as a computer that stores information and waits for requests. When you visit a website, your browser asks a server, "Can I see this webpage?" and the server responds by sending back the webpage data.

Why do we need to talk to servers?

  • - To get data (like weather information, user profiles, or news articles)

  • - To send data (like submitting a form or uploading a file)

  • - To update data (like changing your profile picture)

  • - To delete data (like removing a post) Every time you use an app or website, there's communication happening between your device and a server.

What is cURL?

cURL stands for "Client URL," and it's a tool that lets you send messages to servers directly from your terminal or command line.

Think of it this way: -

Browser: A fancy, visual way to talk to servers (with buttons, images, and colors) - cURL: A simple, text-based way to talk to servers (just commands and text responses) Both do the same job—they send requests to servers—but cURL gives you more control and is perfect for: -

Testing if a server is working

  • - Automating tasks

  • - Building and testing your own applications

  • - Working with APIs


Why programmers need cURL

Here are the main reasons programmers use cURL:

  1. Testing APIs Without Writing Code:-

    Before building a full application, you can quickly test if an API works using a simple cURL command.

  2. Debugging:-

    When something goes wrong, cURL helps you see exactly what's being sent to and received from a server.

  3. Automation

    You can write scripts that use cURL to perform tasks automatically (like checking if a website is up every hour).

  4. Learning

    cURL helps you understand how web communication works behind the scenes.

  5. Speed

    It's much faster to test something with a one-line cURL command than to build an entire application.


Making your first request using cURL

Let's start with the simplest possible cURL command. Open your terminal and type:

bash curl https://example.com

What happens?

  1. cURL sends a request to the server at example.com

  2. The server sends back the HTML code of the webpage

  3. cURL displays that HTML in your terminal

Understanding Request and Response

There's a conversation between computer (client) and the server.

What's in a Request?

When cURL sends a request, it includes: -

URL: Where to send the request (e.g., https://api.example.com/users

Method: What action to take (GET, POST, etc.)

Headers: Additional information (like "I accept JSON data")

Body: Data you're sending (like form information)

What's in a Response?

The server sends back: -

Status Code:- Did it work? (200 = success, 404 = not found, 500 = server error)

Headers: Information about the response

Body: The actual data (HTML, JSON, XML, etc.)


Using cURL to talk to APIs

APIs (Application Programming Interfaces) are like menus at a restaurant. They tell you what you can request from a server and how to request it.

GET Request: Fetching Data

The most common action—getting information from a server.

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

This fetches information about a GitHub user named "Awdhesh9860" and returns it as JSON data.

{
"type": "User",
"user_view_type": "public",
"site_admin": false,
"name": "Awdhesh Kumar",
"company": null,
"blog": "",
"location": null,
"email": null,
"hireable": null,
"bio": "MERN Stack Developer | React.js | Node.js | Express.js | MongoDB | Building Scalable Web Applications",
"twitter_username": null,
"public_repos": 22,
"public_gists": 0,
"followers": 0,
"following": 1,
"created_at": "2023-09-14T05:17:07Z",
"updated_at": "2026-01-07T12:11:08Z"
}

POST Request: Sending Data

When you need to send data to a server (like creating a new user or posting a comment).

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

Let's break this down: -

X POST`: Specifies the POST method

H: Adds a header telling the server we're sending JSON

d: The data we're sending

Response:

{
  "title": "My Post",
  "body": "This is my post content",
  "userId": 1,
  "id": 101
}

Basic HTTP Request and Response Structure

More from this blog

Awdhesh

10 posts