DEV Community

Cover image for Getting Started with cURL
Mohd Asif Ansari
Mohd Asif Ansari

Posted on

Getting Started with cURL

If you're learning backend development, you've probably heard "just cURL it." But what is cURL?

Let me explain it simply.

What is a Server?

A server is a computer that waits for requests and sends back responses.

Example:

  • You type "google.com"
  • Browser asks Google's server for the page
  • Server sends back the webpage
  • Browser shows it

What is cURL?

cURL is a tool that lets you talk to servers from your terminal.

Simple version:

  • Browser talks to servers by clicking and typing URLs
  • cURL talks to servers through commands

Why use cURL?

  • Test your API quickly
  • See raw data servers send back
  • Faster than opening a browser
  • Good for learning how HTTP works

Your First cURL Command

Open your terminal and type:

curl https://example.com
Enter fullscreen mode Exit fullscreen mode

You'll see HTML code. That's it! You just fetched a webpage.

What happened:

  1. cURL asked example.com for the page
  2. Server sent back HTML
  3. cURL showed it in terminal

Request and Response

Every cURL command has two parts:

Request: What you send to the server

Response: What the server sends back

Your Terminal → Request → Server
Server → Response → Your Terminal
Enter fullscreen mode Exit fullscreen mode

Using cURL with APIs

APIs send back data (usually JSON) instead of webpages.

GET Request (Getting Data)

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

You'll get:

{
  "id": 1,
  "name": "Leanne Graham",
  "email": "leanne@example.com"
}
Enter fullscreen mode Exit fullscreen mode

That's API data!

POST Request (Sending Data)

curl -X POST https://jsonplaceholder.typicode.com/posts \
  -H "Content-Type: application/json" \
  -d '{"title":"My Post","body":"Hello"}'
Enter fullscreen mode Exit fullscreen mode

What this means:

  • -X POST - We're creating something
  • -H - Telling server we're sending JSON
  • -d - The actual data

GET vs POST

GET - Asking for data

curl https://api.com/users
Enter fullscreen mode Exit fullscreen mode

POST - Sending data

curl -X POST https://api.com/users -d '{"name":"John"}'
Enter fullscreen mode Exit fullscreen mode

Simple rule:

  • Fetching info? Use GET
  • Creating/updating? Use POST

Useful Examples

Check if website is working:

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

Save response to a file:

curl https://api.com/data > output.json
Enter fullscreen mode Exit fullscreen mode

See detailed info:

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

Common Mistakes

1. Forgetting quotes around JSON:

# Wrong
curl -d {title:test} https://api.com

# Right
curl -d '{"title":"test"}' https://api.com
Enter fullscreen mode Exit fullscreen mode

2. Not using POST for creating data:

# Add -X POST when creating
curl -X POST https://api.com/users -d '{"name":"John"}'
Enter fullscreen mode Exit fullscreen mode

3. Missing Content-Type for JSON:

curl -X POST https://api.com/data \
  -H "Content-Type: application/json" \
  -d '{"key":"value"}'
Enter fullscreen mode Exit fullscreen mode

When to Use cURL

Testing your API:

curl http://localhost:3000/api/users
Enter fullscreen mode Exit fullscreen mode

Checking if an API works:

curl https://api.github.com/users/octocat
Enter fullscreen mode Exit fullscreen mode

Quick debugging:

curl https://yourapp.com/endpoint
Enter fullscreen mode Exit fullscreen mode

Quick Reference

Basic request:

curl https://example.com
Enter fullscreen mode Exit fullscreen mode

POST with JSON:

curl -X POST https://api.com/data \
  -H "Content-Type: application/json" \
  -d '{"key":"value"}'
Enter fullscreen mode Exit fullscreen mode

Save to file:

curl https://example.com > file.html
Enter fullscreen mode Exit fullscreen mode

Summary

cURL = Tool to send requests from terminal

Two main types:

  • GET - Get data
  • POST - Send data

Basic pattern:

curl [URL]
Enter fullscreen mode Exit fullscreen mode

Start simple. Try fetching a page. Then try an API. Build from there.

Top comments (0)