DEV Community

Clybotics
Clybotics

Posted on Originally published at clybotics.com on

HTTP Status Codes Cheat Sheet: What They Mean and What to Check

HTTP status codes cheat sheet with common 2xx, 3xx, 4xx and 5xx codes and what to check next

A good HTTP status codes cheat sheet should do more than tell you that 404 means "Not Found." When a request fails, you need to know what to check next, which system sent the response and whether it's safe to try again.

This guide gives you the codes you'll see most often, the pairs people mix up, a few curl commands and a simple way to trace a problem. A status code won't always tell you the cause, but it can tell you where to start looking.

Key takeaways

  • Record the method, URL, time, status, headers, response body and request ID before you change anything.
  • A 401 asks for valid authentication. A 403 means the server refuses the request, and signing in may not help.
  • A 502 means a gateway got an invalid response. A 504 means it didn't get one in time.
  • A 5xx response doesn't prove that the request did nothing. Check before you send a POST again.
  • A 200 response can still contain an application error, so read the body too.

HTTP status codes cheat sheet

The first digit tells you the general type of response. If your client doesn't recognize a code, the HTTP standard, RFC 9110, says to treat it like the first code in its group. For example, an unknown 4xx code should be treated like 400.

2xx: the request succeeded

Code What it means What to check
200 OK The request succeeded Does the body contain the result you expected?
201 Created Something new was created If there's a Location header, does it point to the new item?
204 No Content It worked, and there's no response body Is your code trying to read JSON that isn't there?

3xx: look at a redirect or saved copy

Code What it means What to check
301 or 308 The address has changed permanently Is the Location header right, and should the method stay the same?
302 or 307 Use a different address for now Is this really temporary, and should the method stay the same?
304 Not Modified The saved copy can still be used Does the client have that copy?

A 304 is in the 3xx group, but it doesn't send you to another URL. It tells the client to use the response it already has.

4xx: check the request, access and address

Code What it means What to check
400 Bad Request The server sees a problem with the request Its format, headers and any proxy that changed it
401 Unauthorized Valid authentication is missing The token and the WWW-Authenticate header
403 Forbidden The server refuses the request Permissions, firewall rules and account limits
404 Not Found Nothing was found, or the server won't say it's there The URL, route, ID, environment and permissions
405 Method Not Allowed This address doesn't support that method The Allow header
409 Conflict The request conflicts with the current data Duplicates, versions and recent changes
415 Unsupported Media Type The server doesn't accept this format Content-Type and Content-Encoding
422 Unprocessable Content The format is fine, but the server can't follow the instructions Required values and business rules
429 Too Many Requests You've reached a rate limit The Retry-After header and the API's limits

5xx: trace what happened on the server side

Code What it means What to check
500 Internal Server Error Something unexpected happened Error logs and recent changes
502 Bad Gateway A gateway got an invalid response Gateway logs, connection resets and broken responses
503 Service Unavailable The service is overloaded or being maintained Service health, capacity and Retry-After
504 Gateway Timeout A gateway didn't get a response in time Slow work, connections and timeout settings

The codes people mix up

These pairs can look similar, but they send you in different directions.

401 vs 403

Start with authentication for a 401. The token may be missing, expired or rejected. The response must include a WWW-Authenticate header with at least one way to authenticate.

A 403 means the server understood the request but won't carry it out. Often, the user is signed in but doesn't have permission. That isn't always the reason, though. A firewall or another rule can also return 403. Some servers send a 404 instead so they don't reveal that a private item exists.

502 vs 504

A gateway or proxy passes a request to another server. It sends a 502 when it gets an invalid response and a 504 when it doesn't get one in time. Don't turn that into "502 means crashed and 504 means slow." For example, AWS's load balancer guide says that some Lambda timeouts return 502, while an incorrect Content-Length can lead to a 504.

301 vs 302 vs 307 vs 308

You need to know two things: is the move permanent, and should the method stay the same? With 301 or 302, a client may change a POST to a GET. Use 307 for a temporary move or 308 for a permanent one when the method must stay the same. If you want the client to fetch a result page with GET after a form is submitted, use 303.

This affects SEO too. Google's redirect guide treats permanent redirects as a strong sign that the new URL should be the main one. Temporary redirects are a weaker sign.

404 vs 410

A 404 doesn't say whether the page is gone for good. A 410 says it probably is. For Google Search, both can lead to the URL being removed from the index over time, so you don't need to change every old 404 to a 410.

A "page not found" message shouldn't return 200. Google may report it as a soft 404. That's Google's name for the problem, not an HTTP status code.

Check the response with curl

These commands use example.com, and the third one uses Bash syntax. If you're using Windows PowerShell 5.1, type curl.exe instead of curl. In the third command, also change /dev/null to NUL.

# Show the response headers and body
curl -i https://example.com/

# Send a HEAD request to get only the headers
curl -I https://example.com/

# Follow up to 5 redirects and show where you end up
curl -sS -L --max-redirs 5 -o /dev/null \
  -w 'status=%{http_code} final=%{url_effective}\n' \
  https://example.com/
Enter fullscreen mode Exit fullscreen mode

The -I option sends a HEAD request, not a GET. A server can handle the two differently, so use the original method when you're trying to reproduce a problem. Start without -L, so you can see the first response before you follow any redirects. Don't repeat a request that changes data just to inspect it.

For more detail, you can add -v. Its output can include authorization headers and cookies, so remove anything private before you share it. If no HTTP response arrived, there's no status code to investigate. Check DNS, the connection, TLS and client timeouts instead.

Find out which system sent the error

Your app may not have sent the response you see. A content delivery network (CDN), proxy or load balancer could have created it. Seeing a provider's name in the Server header doesn't prove that the provider caused the error.

Request path from a client through a CDN, proxy, app and database, with questions for tracing an HTTP 502 or 504 error

Compare the logs for the same request. The client and your app may have recorded different results.

  1. Save the details. Record the method, URL, time with its time zone, status, safe headers and request ID.
  2. Read the body. It may contain a more specific error. Some APIs use the problem details format in RFC 9457.
  3. Follow each redirect. Check the address and method at every step.
  4. Compare the logs. Find the same request in the CDN, proxy, app and dependency logs.
  5. Test one possible cause. If you change five things at once, you won't know which one helped.
  6. Check the fix. Use the original method in a safe environment, then make sure the data is right.

In nginx, compare $status with $upstream_status. The first is the status nginx sent to the client. The second usually comes from the server behind it. There's an important exception: nginx can also record 502 in $upstream_status when it can't choose a server. On its own, that value doesn't prove your app sent a 502.

Is it safe to retry the request?

The cover suggests trying again after a short while for a 5xx error. That's fine for many requests that only read data. It can be risky for requests that create an order, take a payment or send a message.

Here's a made-up example. A customer submits POST /orders, and the proxy returns a 504. The app log shows that the order was saved a moment after the proxy gave up. If the client sends the POST again, the customer could end up with two orders.

First, look up the order using a reference the client already has. To prevent the problem, give each order attempt a unique idempotency key. If the same key arrives again, the server should return the first result instead of creating another order. The server has to support this, because simply adding a header won't do anything. You'll also need to find out why the app was slow. A longer timeout may only hide the problem.

In HTTP, GET, HEAD, PUT and DELETE are defined as idempotent. That means sending the same request several times should have the same intended effect as sending it once. The responses may be different. POST isn't idempotent by default, but an API can make specific POST requests safe to repeat. Always check how the API really behaves.

Response What to do before retrying
400, 404, 405, 415 or 422 Fix the request or address. Sending it again unchanged usually won't help.
401 Get valid credentials, then make sure it's safe to repeat the request.
403 Check why access was refused. Don't keep retrying with the same credentials.
409 Get the latest data and resolve the conflict.
429 or 503 Wait at least as long as Retry-After says, if it's there. Make sure it's safe to repeat the request.
500, 502 or 504 Find out whether the first request worked. Retry only if the problem looks temporary and it's safe to repeat.

If you automate retries, limit how many times you try and how long you keep going. Wait longer after each attempt and add a small random delay. Otherwise, lots of clients can retry together and overload the service again.

Don't stop at the status code

A 200 response tells you about the HTTP request. It doesn't prove that the business task worked. An API can return 200 with a body that says the payment was declined, so your code needs to check both.

If you design the API, choose a status that matches the result and return an error code your client can understand. Include a request ID, but leave out stack traces, database details and other private information. Clear errors are also important at checkout. Our ecommerce checkout guide explains how to help shoppers recover without paying twice.

Conclusion: follow one request

The next time something fails, pick one request and follow it from the client to the system that responded. Use the status code to choose your first check, then use the headers, body and logs to find the cause. Before you press retry, make sure the first attempt didn't already work.

If errors keep getting lost between several systems, take a look at our API development and integration services or tell us which request is failing. Please remove tokens, cookies and customer data first.

Frequently Asked Questions

What is the difference between 401 and 403?

A 401 means valid authentication is missing, so check the token and the WWW-Authenticate header. A 403 means the server refuses the request. The user may not have permission, or a firewall or another rule may be blocking it.

What is the difference between 502 and 504?

A 502 means a gateway got an invalid response from another server. A 504 means it did not get a response in time. The code alone does not tell you the cause, so compare the gateway and app logs.

Should I use a 301, 302, 307 or 308 redirect?

Use 301 or 308 for a permanent move and 302 or 307 for a temporary one. Choose 307 or 308 when the method must stay the same. With 301 or 302, a client may change a POST to a GET.

Is a 304 status code an error?

No. A 304 tells the client that its saved copy can still be used, so the server does not send the content again. It is in the 3xx group, but it does not send the client to another URL.

Can a 200 OK response contain an error?

Yes. The HTTP request can succeed while the response body reports an application error, such as a declined payment. Check both the status code and the body.

Is it safe to retry a 500, 502 or 504 error?

It depends on the request. Reading data again is often safe, but repeating a POST could create a second order or payment. First, find out whether the original request worked or whether the API safely handles duplicates.

Top comments (0)