DEV Community

Rajesh Kumar
Rajesh Kumar

Posted on

Getting Started with cURL: Sending Messages to the Internet

Imagine you want to order food from a restaurant. You don't always go there yourself; sometimes you send a message or make a call to their "Service Counter." In the world of programming, that restaurant is a Server, and cURL is the phone you use to talk to it.

As a developer, you’ll spend a lot of time "talking" to servers. Let’s break down how to do that without any fancy browser, just using your terminal.

cURL vs Browser

1. What is cURL (In very simple terms)

cURL stands for "Client URL." Think of it as a web browser that has no buttons, no images, and no colors. It is a command-line tool that lets you send and receive data from a server using a URL.

If a browser is like a TV where you see everything, cURL is like a Walkie-Talkie. You send a specific "request" to a server, and it sends back a "response" in plain text.

2. Why programmers need cURL

Why bother with a black-and-white terminal when you have Chrome or Safari?

  • Speed: It’s much faster to type a command than to open a browser and click through menus.
  • Automation: You can write scripts to call a server 100 times in a row.
  • Testing APIs: Programmers use it to check if their "Backend" (the hidden logic of an app) is working correctly before the "Frontend" (the screen) is even built.

Where cURL fit in Background

3. Making your first request using cURL

Let's keep it simple. Open your terminal (Command Prompt or Terminal) and type this:

Bash

curl https://www.google.com
Enter fullscreen mode Exit fullscreen mode

What happened? You just sent a message to Google’s server saying, "Give me your homepage." Instead of showing you the colorful logo, the server sent you the raw HTML code. That code is what your browser usually turns into a pretty webpage.

4. Understanding Request and Response

Every time you use cURL, a two-part conversation happens:

  1. The Request: This is what you send. It includes the URL and sometimes "headers" (extra info like what language you speak).

  2. The Response: This is what the server sends back. It usually contains:

  • Status Code: (e.g., 200 OK means success, 404 Not Found means you're lost).
  • Body: The actual data (HTML or JSON).

Basic

5. Using cURL to talk to APIs

Most modern apps talk in JSON (a simple text format). Let’s try two basic ways to talk to an API:

The GET Request (Asking for data)

This is like asking, "What's on the menu?"

Bash

curl https://jsonplaceholder.typicode.com/posts/1
Enter fullscreen mode Exit fullscreen mode

The server will send back a small piece of data about a "post."

The POST Request (Sending data)

This is like saying, "I want to place an order." We use the -X POST flag and -d for "data."

Bash

curl -X POST https://jsonplaceholder.typicode.com/posts -d "title=MyNewPost"
Enter fullscreen mode Exit fullscreen mode

You just "pushed" information to the server.

curl

6. Common mistakes beginners make

Even intermediate developers trip up on these:

  • Forgetting the Protocol: Always include https://. If you just type google.com, cURL might get confused.
  • Invisible Characters: If you copy-paste a command from a blog, sometimes "smart quotes" (“ ”) replace straight quotes (" "), and the command will fail.
  • Ignoring Errors: If nothing happens, add -v (verbose) to your command. It’s like turning on the lights in a dark room—it shows you every detail of the conversation.
Bash

curl -v https://google.com
Enter fullscreen mode Exit fullscreen mode

Resources

Top comments (1)

Collapse
 
charanpool profile image
Charan Koppuravuri

Informative :)