Status code families: 2xx, 3xx, 4xx and 5xx
35 min · Beginner
Need help? Ask a tutor on WhatsApp (opens in a new tab)A client cannot read every error message. The three-digit number on the first line of the response tells it at a glance whether to carry on, look elsewhere, fix its request or wait for the server.
The five families
Status code is the three-digit number in the first line of every HTTP response. Its first digit sets the family: 1xx informational, 2xx success, 3xx redirection, 4xx client error, 5xx server error.
Four requests, one for each of four families. head -1 keeps only the status line of each answer.
Example 1 · Four families in four requests
curl -si http://localhost:8080/api/v1/products/6 | head -1curl -si http://github.com | head -1curl -si http://localhost:8080/api/v1/products/99 | head -1curl -si https://httpbin.org/status/500 | head -1$ curl -si http://localhost:8080/api/v1/products/6 | head -1 HTTP/1.1 200 OK $ curl -si http://github.com | head -1 HTTP/1.1 301 Moved Permanently $ curl -si http://localhost:8080/api/v1/products/99 | head -1 HTTP/1.1 404 Not Found $ curl -si https://httpbin.org/status/500 | head -1 HTTP/1.1 500 INTERNAL SERVER ERROR
What this code does
-shides curl's own messages and-iincludes the response head;| head -1keeps only the first line, the status line. (headworks in Git Bash and macOS Terminal.)- One request per family:
200success,301redirect (plainhttp://github.commoves you to https),404client error (product 99 does not exist),500server error (httpbin's/status/500answers with whatever code you ask for). - httpbin writes its reason phrase in capitals. Programs read only the number, so that does not matter.
A client that does not know a particular code, say 418, still knows its family and handles it like the family's x00 code. The families:
1xxinformational: "keep going" (100 Continue,101 Switching Protocolsfor WebSockets). Rare in API work.2xxsuccess: the request worked.3xxredirection: look somewhere else, or use your saved copy.4xxclient error: the request is wrong; sending it again unchanged fails again.5xxserver error: the request may be fine; the server failed.
2xx: 200 OK, 201 Created, 204 No Content
200 / 201 / 204 are the common success codes: 200 OK for a read or update that worked, 201 Created for a new resource, and 204 No Content for success with nothing to send back.
A POST that creates a product, then a DELETE that removes it.
Example 2 · 201 Created, then 204 No Content
curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI10","name":"Tripod Stand 10in","categoryId":4,"price":399.00,"stock":20}'curl -i -X DELETE http://localhost:8080/api/v1/products/16$ curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI10","name":"Tripod Stand 10in","categoryId":4,"price":399.00,"stock":20}'
HTTP/1.1 201 Created
Date: Fri, 16 Oct 2026 02:40:56 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 140
Location: http://localhost:8080/api/v1/products/16
{"id":16,"sku":"ACC-TRI10","name":"Tripod Stand 10in","categoryId":4,"categoryName":"Accessories","price":399.00,"stock":20,"isActive":true}
$ curl -i -X DELETE http://localhost:8080/api/v1/products/16
HTTP/1.1 204 No Content
Date: Fri, 16 Oct 2026 02:40:57 GMTWhat this code does
- The POST answers
201 CreatedwithLocation: http://localhost:8080/api/v1/products/16and the new product in the body. - The DELETE answers
204 No Content: noContent-Type, noContent-Length, no body. Success is told by the status line alone.
A good API adds a Location header with the new item's URL to a 201 and returns the item in the body. A 204 has no body at all, so a client must not try to read JSON from it.
3xx: redirects and 304 Not Modified
301 / 302 are redirect codes: the client repeats the request at the URL in the Location header. 301 Moved Permanently means for good, so links should be updated; 302 Found means for now.
Two redirects, one followed with -L, then a conditional request that gets 304.
Example 3 · 301 and 302 redirects, followed with -L
curl -i http://github.comcurl -i -L https://httpbin.org/redirect/1$ curl -i http://github.com
HTTP/1.1 301 Moved Permanently
Content-Length: 0
Location: https://github.com/
$ curl -i -L https://httpbin.org/redirect/1
HTTP/1.1 302 FOUND
Date: Fri, 16 Oct 2026 08:06:15 GMT
Content-Type: text/html; charset=utf-8
Content-Length: 215
Connection: keep-alive
Server: gunicorn/19.9.0
Location: /get
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 08:06:17 GMT
Content-Type: application/json
Content-Length: 256
Connection: keep-alive
Server: gunicorn/19.9.0
Access-Control-Allow-Origin: *
Access-Control-Allow-Credentials: true
{
"args": {},
"headers": {
"Accept": "*/*",
"Host": "httpbin.org",
"User-Agent": "curl/8.21.0",
"X-Amzn-Trace-Id": "Root=1-6ad1daf7-0e74597a6c887b034c8e13c1"
},
"origin": "203.0.113.25",
"url": "https://httpbin.org/get"
}What this code does
- Without
-L, curl shows the 301 and stops. The answer has an empty body (Content-Length: 0); the useful part isLocation: https://github.com/. - With
-L, curl follows the 302 by itself: it sends a new GET to/get(a relativeLocation, completed from the first URL) and prints both responses, each with its own status line. The 302's small HTML body is not printed when curl follows it.
Example 4 · 304 Not Modified with ETag and If-None-Match
curl -i http://localhost:8080/api/v1/products/6curl -i http://localhost:8080/api/v1/products/6 -H 'If-None-Match: "p6-v1"'$ curl -i http://localhost:8080/api/v1/products/6
HTTP/1.1 200 OK
Date: Fri, 16 Oct 2026 02:40:59 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 141
ETag: "p6-v1"
Cache-Control: no-cache
{"id":6,"sku":"ACC-AD141","name":"boAt Airdopes 141","categoryId":4,"categoryName":"Accessories","price":1299.00,"stock":120,"isActive":true}
$ curl -i http://localhost:8080/api/v1/products/6 -H 'If-None-Match: "p6-v1"'
HTTP/1.1 304 Not Modified
Date: Fri, 16 Oct 2026 02:40:59 GMT
ETag: "p6-v1"
Cache-Control: no-cacheWhat this code does
- The first answer carries
ETag: "p6-v1", a version tag for product 6. A client saves the body together with this tag. - The second request asks "send it only if it is no longer version p6-v1". It has not changed, so the server answers
304with no body, and the client uses its saved copy.
Browsers follow redirects on their own; curl does only with -L.
304 Not Modified is different: it says "your saved copy is still good". The client sends the ETag (a version tag) it got earlier in an If-None-Match header. If the resource has not changed, the server answers 304 with no body, saving the download.
303 See Other (after a form POST, go GET this page) and 307 / 308, which work like 302 / 301 but promise to keep the method and body.4xx: the client must fix the request
4xx is the family of client-error codes: the server understood that something is wrong with the request itself. Repeating the request unchanged gives the same error, so the client must change something first.
Two pairs that people mix up: a broken body versus a body that breaks a rule (400 and 422), and a missing login versus a missing permission (401 and 403).
Example 5 · 400 versus 422: broken versus against the rules
curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":42,"price":449.00,"stock":5'curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":42,"price":449.00,"stock":5}'$ curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":42,"price":449.00,"stock":5'
HTTP/1.1 400 Bad Request
Date: Fri, 16 Oct 2026 02:40:59 GMT
Content-Type: application/problem+json
Content-Length: 180
{"type":"https://shoplite.example/problems/malformed-request","title":"Malformed request","status":400,"detail":"The request body is not valid JSON.","instance":"/api/v1/products"}
$ curl -i http://localhost:8080/api/v1/products -H "Content-Type: application/json" -d '{"sku":"ACC-TRI12","name":"Tripod 12in","categoryId":42,"price":449.00,"stock":5}'
HTTP/1.1 422 Unprocessable Content
Date: Fri, 16 Oct 2026 02:40:59 GMT
Content-Type: application/problem+json
Content-Length: 169
{"type":"https://shoplite.example/problems/rule-broken","title":"Business rule broken","status":422,"detail":"Category 42 does not exist.","instance":"/api/v1/products"}What this code does
- The first body is missing its closing
}. The server cannot even read it:400 Bad Request. Errors use one standard JSON shape, Problem Details (application/problem+json):type,title,status,detailandinstance. - The second body is valid JSON and every field is fine on its own, but category 42 does not exist:
422 Unprocessable Content(older name: Unprocessable Entity). A field that breaks its own rule, such as a price of 0, is still a400in ShopLite; some APIs use 400 for everything and let the error body explain.
Example 6 · 401 versus 403: who are you, and are you allowed
curl -i https://api.fest.example/v1/admin/statscurl -i https://api.fest.example/v1/admin/stats -H "Authorization: Bearer learner-token"$ curl -i https://api.fest.example/v1/admin/stats
HTTP/1.1 401 Unauthorized
Date: Fri, 16 Oct 2026 02:41:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 60
WWW-Authenticate: Bearer realm="fest"
{"error":"Log in first: send Authorization: Bearer <token>"}
$ curl -i https://api.fest.example/v1/admin/stats -H "Authorization: Bearer learner-token"
HTTP/1.1 403 Forbidden
Date: Fri, 16 Oct 2026 02:41:00 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 43
{"error":"Only admins can see sales stats"}What this code does
- With no credentials, the server does not know who is asking:
401plusWWW-Authenticate: Bearer, which says "send a Bearer token". - With a valid learner token, the server knows the user, but sales stats are for admins only:
403 Forbidden. Logging in again will not help; only different permissions will.
The 4xx codes you will meet most:
400broken request (bad JSON, missing field, price 0);422well-formed and valid field by field, but against a rule involving other data (a category id that does not exist)415wrong body type (Content-Type);409clashes with the current state (duplicate SKU, order already cancelled)401not logged in or bad token: "who are you?";403logged in but not allowed: "I know you, and no"404no such resource;405the resource exists but not for this method (seeAllow)429too many requests: slow down and wait for theRetry-Afterseconds
Predict the output
Which 4xx code?
Choose the status code the server should send.
Round 1 of 4
Which status code?
401 is called "Unauthorized" but really means unauthenticated: the server does not know who you are. It comes with a WWW-Authenticate header that says how to log in.5xx: the server failed, and who must fix what
500 / 502 / 503 / 504 are server-error codes: the request may be fine, but the server could not handle it. 500 is a bug in the app, 502 a bad answer through a proxy, 503 unavailable, 504 a proxy timeout.
A payments service in maintenance answers 503 and says in Retry-After how many seconds to wait.
curl -i https://api.payments.example/v1/statusHTTP/1.1 503 Service Unavailable
Date: Fri, 16 Oct 2026 02:41:02 GMT
Content-Type: application/json; charset=utf-8
Content-Length: 67
Retry-After: 120
{"error":"Payments are under maintenance. Try again in 2 minutes."}A 5xx cannot be fixed by changing the request; the people running the server must act. The client can only retry later, and only safely for idempotent requests. Who looks at what:
500 Internal Server Error: the developers, from the server logs.502 Bad Gateway: whoever runs the gateway or the app behind it (often the app is down).503 Service Unavailable: wait; the server is overloaded or in maintenance and often sendsRetry-After.504 Gateway Timeout: the proxy waited too long for the app or a service behind it.
Next step
Check your understanding
12 quick questions