Status Codes Tell a Story
The right code helps clients handle responses correctly.
2xx - Success
200 OK - Request succeeded
201 Created - Resource created (POST)
204 No Content - Success, nothing to return (DELETE)
return response()->json($user, 200);
return response()->json($newUser, 201);
return response()->noContent(); // 204
3xx - Redirection
301 Moved Permanently - URL changed forever (SEO)
302 Found - Temporary redirect
304 Not Modified - Use cached version
4xx - Client Errors
400 Bad Request - Malformed request
401 Unauthorized - Not authenticated
403 Forbidden - Authenticated but not allowed
404 Not Found - Resource doesn't exist
405 Method Not Allowed - Wrong HTTP method
409 Conflict - Conflicting with current state
422 Unprocessable Entity - Validation failed
429 Too Many Requests - Rate limited
// Common usage
abort(404);
abort(403, 'You cannot edit this post');
return response()->json([
'message' => 'Validation failed',
'errors' => $validator->errors()
], 422);
5xx - Server Errors
500 Internal Server Error - Generic server error
502 Bad Gateway - Upstream server error
503 Service Unavailable - Server overloaded/maintenance
504 Gateway Timeout - Upstream timeout
Quick Reference
| Action | Success | Error | |--------|---------|-------| | GET item | 200 | 404 | | GET list | 200 | - | | POST create | 201 | 422 | | PUT/PATCH update | 200 | 404, 422 | | DELETE | 204 | 404 |
