Ping vs cURL: Talking to Servers
- ping
- curl
- HTTP
- status codes
- ICMP
- debugging
- APIs
- JSON
Ping vs cURL - Is It Up, and Is It Working? ๐ฐ๏ธ
4:55 PM, Friday. Slack lights up: "Is the API down?? Nothing's working!" Everyone panics. You calmly type two commands and, in 10 seconds, know exactly whose problem it is. That calm is a skill - here it is.
๐ง The mental model: a building
ping= knocking on the building's front door. Is anyone home? Is the building even standing? It tells you the machine is reachable - nothing about your app.curl= walking in and asking the receptionist a real question. Does the service actually answer correctly?
ping - "is the host reachable?" (Layer 3)
ping sends a tiny ICMP echo and times the round trip:
ping example.com
# 64 bytes from 93.184.216.34: time=12.3 ms โ
reachable, ~12 ms away
curl - "does the request actually work?" (Layer 7)
curl makes a real HTTP request and shows you the response - status, headers, body. This is how you test an API by hand:
curl -i https://example.com/health
# HTTP/2 200
# content-type: application/json
# {"status":"ok"}
That {"status":"ok"} body is JSON (JavaScript Object Notation): plain text built from objects in curly braces, arrays in square brackets, double-quoted keys and simple values (numbers, strings, true, false, null). It is the format most web APIs send and receive, and Python turns it into dicts and lists with json.loads(), or response.json() in requests.
๐ The killer insight: ping works but curl fails โ the server is up, but your app is broken (crashed service, wrong path, missing auth). That one distinction saves hours of blaming the network.
HTTP status codes - read them like an X-ray
The first digit tells you the whole story:
| Range | Meaning | The one-word version | Examples |
|---|---|---|---|
| 2xx | Success | "great" | 200 OK, 201 Created |
| 3xx | Redirect | "go look over there" | 301 Moved, 302 Found |
| 4xx | Client error | "YOU messed up" | 401 Unauthorized, 403 Forbidden, 404 Not Found |
| 5xx | Server error | "THEY messed up" | 500 Error, 503 Unavailable |
๐ง Remember just this: 4xx = YOU, 5xx = THEM. A 4xx means fix your request (bad URL, missing key, no auth). A 5xx means the server crashed - check its logs, not your code. (Fun one to cement it: 418 = "I'm a teapot", a real joke status code. If you remember the teapot, you remember 4xx = client.)
The curl flags you'll actually use
# -i = show response headers (and the status code)
curl -i URL
# -X = choose the method (GET, POST, ...)
curl -X POST URL
# -H = add a header (e.g. auth)
curl -H "Authorization: Bearer TOKEN" URL
# -d = send a request body (it makes the request a POST); the -H header says the body is JSON
curl -H "Content-Type: application/json" -d '{"qty":10}' URL
๐ง Try it for real
# ctrl-C to stop. See your latency.
ping google.com
# a real public API - read the status line
curl -i https://api.github.com
Your Task
Write classify(status) to label any HTTP code by category, then print the classification for a few codes. Predict first: is 404 a "you" or "them" problem?
Related terms in the glossary