Keyboard Shortcuts N Next post
P Previous post
S Save / unsave
R Read aloud
T Toggle theme
/ Focus search
Esc Close panels
🔥
Ready to read...
API Basics APIs APIs From Zero to Real-World API Testing & Automation HTTP Module 1 — API Fundamentals

HTTP Status Codes Every Developer Should Know

Reviewed & accurate
AI Summary

What You'll Learn

  • The five families of HTTP status codes (1xx–5xx)
  • The specific codes you will see in real API work
  • How to choose the right success code for your API
  • How to read error codes to debug faster

Why This Matters

Every HTTP response starts with a status code. It is the single most important piece of information in the response — it tells you whether your request worked, failed, or something in between. If you do not know what 401 vs 403 vs 422 mean, you will misdiagnose every API problem.

The Five Families

Status codes are three-digit numbers. The first digit tells you the family:

RangeFamilyMeaning
100–199InformationalRarely used in modern APIs
200–299SuccessThe request worked
300–399RedirectionThe resource moved; client should follow
400–499Client errorThe client made a bad request
500–599Server errorThe server failed to handle a valid request
Standard reference: Status code definitions are in RFC 9110 § 15. We follow it throughout this lesson.

Success Codes (2xx)

CodeNameWhen to use
200OKGeneric success — GET, PATCH, sometimes POST/PUT
201CreatedPOST created a new resource. Response body usually contains it.
202AcceptedRequest accepted for processing later (async). Common for webhooks.
204No ContentSuccess but no body to return. Typical for DELETE.
206Partial ContentReturned only part of the resource (range requests).

The four you'll see daily

  • 200 OK — "here's what you asked for"
  • 201 Created — "I made the new thing you asked for"
  • 204 No Content — "I did what you asked; nothing to return"
  • 202 Accepted — "I'll do this later; check back"

Redirection Codes (3xx)

CodeNameWhen
301Moved PermanentlyThe resource has a new permanent URL; update your bookmarks.
302FoundTemporary redirect. Use the original URL next time.
304Not ModifiedCached version is still valid; no body returned.

Most APIs avoid 3xx — they prefer to return the resource directly or use a clear 404. But 304 is common in APIs that support caching via If-None-Match and ETag headers.

Client Error Codes (4xx)

This is the family you will spend the most time on. Every 4xx means the client did something wrong.

CodeNameWhat it means
400Bad RequestMalformed syntax — invalid JSON, missing required field.
401UnauthorizedMissing or invalid authentication. Log in first.
403ForbiddenAuthenticated, but you don't have permission for this resource.
404Not FoundThe URL doesn't match any resource.
405Method Not AllowedURL exists, but the method is wrong (e.g., DELETE on a list URL).
409ConflictRequest conflicts with current state — duplicate email, version mismatch.
410GoneResource existed but was permanently deleted (stronger than 404).
415Unsupported Media TypeWrong Content-Type header (e.g., sending XML to a JSON-only API).
422Unprocessable EntityJSON is valid but fails validation (e.g., email format wrong).
429Too Many RequestsYou hit the rate limit. Slow down or wait.

The 401 vs 403 distinction

This is the most-misunderstood pair:

  • 401 Unauthorized: "I don't know who you are." Missing or invalid token. Solution: authenticate.
  • 403 Forbidden: "I know who you are, but you can't do this." Valid token, but no permission. Solution: request access from an admin.

If you are logged in but cannot delete a post, that is 403, not 401. If your token expired and the server does not recognize you, that is 401, not 403.

400 vs 422

  • 400 Bad Request: The request itself is malformed — invalid JSON, missing Content-Type, broken syntax.
  • 422 Unprocessable Entity: The request is well-formed JSON, but the values fail business rules (e.g., email: "not-an-email").

Many APIs use 400 for both cases. Strictly speaking, 422 is more accurate when the JSON parses but fails validation.

Server Error Codes (5xx)

5xx means the server failed. The client's request was valid; the server could not handle it.

CodeNameWhen
500Internal Server ErrorGeneric "something broke on the server".
501Not ImplementedServer doesn't support this method or feature.
502Bad GatewayA proxy/gateway got an invalid response from the upstream server.
503Service UnavailableServer is down for maintenance or overloaded. Try again later.
504Gateway TimeoutA proxy/gateway did not get a response in time from the upstream server.

What 5xx tells you as a client

5xx errors are usually not your fault. Retry with backoff, and if they persist, check the API's status page or contact the provider.

Common Mistakes

  1. Returning 200 for everything. Some APIs return 200 with an error in the body. This breaks standard HTTP clients and tools. Use the right status code.
  2. Using 401 for permission errors. If the user is authenticated but lacks permission, that is 403, not 401.
  3. Using 404 to hide authorization failures. Some APIs return 404 instead of 403 to avoid leaking that a resource exists. This is a deliberate security choice but should be documented.
  4. Treating 5xx as the client's fault. 5xx is the server's problem. Don't try to "fix" your request — retry with backoff.
  5. Forgetting the Location header with 201. 201 Created should include a Location header pointing to the new resource's URL.

Practical Exercise (5 minutes)

Match each scenario to its correct status code:

  1. You send a POST with malformed JSON.
  2. You request a user ID that does not exist.
  3. You forgot to send your auth token.
  4. You send a valid token but try to delete another user's post.
  5. The server's database crashed mid-request.
  6. You successfully created a new user.
  7. You sent 100 requests in 10 seconds and the API blocked you.

Answers: 1→400, 2→404, 3→401, 4→403, 5→500, 6→201, 7→429.

Mini Challenge

Open the JSONPlaceholder API and trigger three different status codes intentionally: a 200 (GET any post), a 404 (GET a non-existent post ID like 999999), and a 201 (POST a new post). Use curl -i so you can see the status line.

Key Takeaways

  • Status codes are three digits. The first digit tells you the family: 1xx info, 2xx success, 3xx redirect, 4xx client error, 5xx server error.
  • Success: 200 (OK), 201 (Created), 204 (No Content), 202 (Accepted).
  • Client errors: 400 (bad request), 401 (unauthenticated), 403 (forbidden), 404 (not found), 422 (validation failed), 429 (rate limited).
  • 401 vs 403: 401 = "I don't know who you are"; 403 = "I know you, but you can't do this".
  • 5xx = server's fault. Retry with backoff; don't try to "fix" your request.
Course continuity
Previously: Lesson 04 covered the five HTTP methods.
Today: You learned what the server sends back as a status code.
Next: In lesson 06 — HTTP Headers, you'll learn the metadata that travels alongside every request and response.

FAQ

Why does my browser show 304 sometimes?

The browser is using cached content. The server is saying "your cached copy is still valid; don't download it again." This is controlled by the If-None-Match and ETag headers, which we cover in lesson 06.

What status code should I use when a user's password is wrong?

401 Unauthorized. The user is attempting to authenticate, and they failed. Some APIs use 400, but 401 is more accurate because the response specifically indicates an authentication failure.

Test Your Knowledge
How did you find this?

Comments

Join the discussion! Sign in with your Google or Blogger account, or comment as Anonymous - no account needed. For quick questions, also reach me on Telegram @cytestch.

Comments