HTTP Status Codes: A Practical Guide
By Urlzee Team · Published · 6 min read
Every HTTP response starts with a three-digit status code that says how the request went. The first digit gives the class: 2xx succeeded, 3xx points somewhere else, 4xx means the request was wrong, 5xx means the server broke. You only need about fifteen codes to understand almost everything you will meet.
What do the five classes of status code mean?
The first digit is the part to learn first, because it tells you whose problem it is.
| Class | Meaning | Who needs to act |
|---|---|---|
| 1xx | Informational; the request is still in progress | Nobody; rarely seen directly |
| 2xx | Success | Nobody |
| 3xx | Redirection; look at another URL | The client follows the Location header |
| 4xx | Client error; the request cannot be fulfilled as sent | Whoever made the request |
| 5xx | Server error; a valid request could not be handled | Whoever runs the server |
A client that meets an unfamiliar code is expected to treat it like the x00 code of its class. An unknown 4xx is handled like a 400, and an unknown 5xx like a 500.
Which success codes should you know?
Three cover nearly all cases.
- 200 OK. The request worked and the response body contains the result.
- 201 Created. The request worked and a new resource now exists. APIs return this from a
POSTthat creates something. The Urlzee API returns 201 when a short link is created, for example. - 204 No Content. The request worked and there is deliberately no body. Common after a delete or an update that has nothing to report.
One trap: a 200 does not prove the content is what you expected. Some sites return 200 with a "page not found" message in the body, known as a soft 404. Monitoring tools that only look at the status code will report such pages as healthy.
What do the redirect codes mean?
A 3xx response says the resource is at a different URL, given in the Location header. The codes differ in whether the move is permanent and whether the request method is kept.
| Code | Name | Permanent? | Keeps POST as POST? |
|---|---|---|---|
| 301 | Moved Permanently | Yes | Not guaranteed |
| 302 | Found | No | Not guaranteed |
| 303 | See Other | n/a | No, always switches to GET |
| 307 | Temporary Redirect | No | Yes |
| 308 | Permanent Redirect | Yes | Yes |
There is also 304 Not Modified, which is not really a redirect. It answers a conditional request and tells the browser its cached copy is still good, so no body is sent.
Short links are redirects. Urlzee answers a short link with a 302 so that every click is counted and an edited destination applies straight away. The reasoning, and how to choose for your own site, is in 301 vs 302 redirects.
What do the common 4xx errors mean?
A 4xx says the request cannot be served as it was sent. Sending the same thing again will get the same answer, so the fix is on the client side.
| Code | Name | Plain meaning | Typical fix |
|---|---|---|---|
| 400 | Bad Request | The server could not understand the request | Fix the syntax, such as malformed JSON |
| 401 | Unauthorized | No valid credentials were provided | Send or correct the API key or login |
| 403 | Forbidden | Understood, but not allowed | Use an account or plan with permission |
| 404 | Not Found | Nothing exists at this URL | Check the address for typos |
| 405 | Method Not Allowed | The URL exists but not for this method | Use the right method, such as GET instead of POST |
| 409 | Conflict | The request clashes with current state | Choose a different value or refresh first |
| 410 | Gone | It existed and was removed on purpose | Stop requesting it; update links |
| 422 | Unprocessable Content | Well-formed, but the values failed validation | Correct the field the error names |
| 429 | Too Many Requests | You hit a rate limit | Wait, then retry more slowly |
A few of these are routinely confused.
401 versus 403. Despite its name, 401 is about authentication: who are you? 403 is about authorisation: you may not do this. A missing API key is a 401. A valid key trying to use a feature its plan does not include is a 403.
404 versus 410. Both mean "nothing here". A 410 adds "and that is intentional and permanent". Search engines treat both as a reason to drop a URL from results; 410 states it more firmly. Urlzee returns 404 for a short link that never existed and 410 for one that has been disabled or has passed its expiry date.
429. This one is temporary. Good APIs send a Retry-After header saying how many seconds to wait. See API rate limiting explained for how to handle it in code.
What do 5xx errors mean?
A 5xx says the request may have been perfectly valid and the server still failed. As a visitor, there is nothing to fix on your side except to try again later.
- 500 Internal Server Error. A generic failure, usually an unhandled exception in the application.
- 502 Bad Gateway. A proxy or load balancer got an invalid response from the server behind it.
- 503 Service Unavailable. The server is overloaded or down for maintenance. It may include
Retry-After. - 504 Gateway Timeout. A proxy waited for the server behind it and gave up.
502, 503 and 504 are usually short-lived. A 500 that repeats for the same request points to a bug.
Which errors should a program retry?
Retry when the cause is temporary, and never retry blindly.
- Retry after waiting: 429, 503, and usually 502 and 504. Honour
Retry-Afterif it is present; otherwise wait a little longer after each attempt. - Do not retry unchanged: 400, 401, 403, 404, 409, 410, 422. Fix the request first.
- Be careful with writes. If a
POSTtimes out, you may not know whether it succeeded. Check before creating the same thing twice. - Cap the attempts. Three to five tries is plenty. A loop without a limit turns one outage into a flood.
How do you check a URL's status code?
In a browser, open the developer tools, go to the Network tab and reload the page. The Status column shows the code for every request.
From a terminal, curl prints just the code with:
curl -s -o /dev/null -w '%{http_code}\n' https://example.com/
Add -L to follow redirects and report the final code instead of the first one. More examples are in how to test redirects with curl.
Without any tools, paste the address into the link checker. It requests the URL from Urlzee's server and lists the status of each hop, the final URL and the response time. A link that works in your browser can still show a 403 there, because some sites refuse automated requests. That is bot blocking, not a broken page.
How do status codes show up in an API?
A well-designed API pairs the status code with a body that explains it.
The Urlzee API, for example, returns errors in one consistent shape:
{ "error": { "code": "alias_taken", "message": "That alias is already taken. Try another." } }
That response arrives with a 409. The same API uses 400 for a missing or malformed JSON body, 401 for a missing or revoked key, 403 when a feature needs a higher plan, 404 for a link that is not yours, 422 when a URL or alias fails validation, and 429 when a limit is exceeded. The complete table is in the API documentation.
When you write a client, branch on the status code first and the error code second, and show the message to a human. For the authoritative definition of every code, MDN keeps a readable reference of HTTP response status codes.
Frequently asked questions
- What are the five classes of HTTP status codes?
- 1xx codes are informational, 2xx mean success, 3xx are redirects, 4xx mean the request was at fault, and 5xx mean the server failed. The first digit tells you the class even when you don't recognise the exact code.
- What is the difference between 401 and 403?
- 401 Unauthorized means the server does not know who you are, usually because credentials are missing or invalid. 403 Forbidden means it knows who you are, or doesn't need to, and the answer is still no.
- What is the difference between 404 and 410?
- 404 Not Found means the server has nothing at that URL and makes no statement about why. 410 Gone means something was there and has been removed on purpose, with no plan to bring it back.
- Should I retry a request that returned a 4xx error?
- Generally no, because the same request will fail the same way until you change it. The main exceptions are 429 Too Many Requests and 408 Request Timeout, which are worth retrying after a wait.