# Understanding HTTP status codes and troubleshooting API errors

Every time your browser or app makes a request to a server, a **status code** is returned: a three-digit number that tells you how the request went. If you can read these codes, you’ll usually know where the problem lies within a minute.

## The five groups

| Starts with | Means | Example |
|---|---|---|
| 1xx | Information | rarely seen |
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirected | 301 and 302 (redirect), 304 Not Modified |
| 4xx | The request is invalid | 401, 403, 404, 419, 422, 429 |
| 5xx | Server error | 500, 502, 503, 504 |

Rule of thumb: **4xx = the question is wrong, 5xx = the answer is wrong.**

## Step 1: Find the status code

**In your browser:** press F12 (Mac: Cmd+Option+I), open the **Network** tab, and repeat the action. Click on the red line. Under **Headers** you will see the status code, and under **Response** the error message from the server.

**In the terminal using curl:**

```bash
curl -i https://jouwsite.nl/api/orders -H "Accept: application/json"
```

`-i` displays the status code and headers. Always include `Accept: application/json` in your API requests. Otherwise, Laravel will redirect you to a web page in the event of an error, rather than returning a clear JSON error.

## Step 2: What does the code mean?

| Code | Meaning | What to look for |
|---|---|---|
| 401 Unauthorised | You are not logged in or your token has expired | Login, API token, cookies |
| 403 Forbidden | You are logged in but do not have permission | Permissions (Policies) in Laravel |
| 404 Not Found | The page or record does not exist | URL, `php artisan route:list` |
| 405 Method Not Allowed | GET sent where POST is expected (or vice versa) | Form method, route |
| 419 Page Expired | Laravel: CSRF token missing or session expired | `@csrf` in the form, session settings |
| 422 Unprocessable | Validation failed | The `errors` field in the response |
| 429 Too Many Requests | Too many consecutive requests | Please wait (see the `Retry-After` header) |
| 500 Internal Server Error | Something went wrong in the code | `storage/logs/laravel.log` |
| 502 Bad Gateway | The server behind the web server is not responding | Is PHP-FPM/the container still running? |
| 503 Service Unavailable | Maintenance or overload | `php artisan up`, server load |
| 504 Gateway Timeout | It took too long | Slow query or external API |

## Step 3: check the logs

In the case of a 500 error, the actual cause is almost always recorded in the log file:

```bash
tail -n 50 storage/logs/laravel.log
```

With Docker: `docker compose logs -f app`. Look for the first line containing `ERROR` and the **stack trace** below it. This will tell you which file and which line is causing the problem.

## Step 4: Provide your AI assistant with the correct information

Don’t just write ‘it doesn’t work’. Please provide:

1. the URL and method (GET/POST),
2. the status code,
3. the response (the error message),
4. the relevant lines from the log file.

With the **http-status-debugging** skill, your assistant will carry out these steps itself before making any changes to the code.

## CORS errors

If you see “blocked by CORS policy” in the console, it means the browser has blocked the response because the server hasn’t authorised your website to fetch it. You can resolve this in `config/cors.php` within your Laravel app, not in the frontend. Never simply set it to `*`` there if you’re working with a login system. See also the guide *HTTP headers: security, caching and CORS*.

## Need help?

Is an error keep cropping up in your live app? Under **Bespoke software and web applications**, I’ll sort this out and prevent it from happening again.

## Skills

- [HTTP status debugging] (/nl/skills/http-status-debugging)
- [Chrome DevTools Agent] (/nl/skills/chrome-devtools)
- [API Design Principles] (/nl/skills/api-design-principles)
- [Web Application Testing] (/nl/skills/webapp-testing)

## Related services

- [Maatwerk software en webapplicaties] (/nl/services#service-03)
- [Webdesign en -ontwikkeling] (/nl/services#service-01)

