Interview question
What HTTP status codes should a REST API return and when? REST API को कौन-से HTTP status codes कब return करने चाहिए?
Answer
| Code | Meaning | When to use |
|---|---|---|
| 200 OK | Success | Successful GET, PUT, PATCH |
| 201 Created | Resource created | Successful POST |
| 204 No Content | Success, no body | Successful DELETE |
| 400 Bad Request | Invalid input | Validation failures |
| 401 Unauthorized | Not authenticated | Missing/invalid token |
| 403 Forbidden | Authenticated but not allowed | Permission denied |
| 404 Not Found | Resource doesn't exist | Invalid ID |
| 422 Unprocessable Entity | Semantic validation error | Field-level validation errors |
| 500 Internal Server Error | Server-side failure | Unhandled exceptions |
return response()->json(['error' => 'User not found'], 404);| Code | अर्थ | कब इस्तेमाल करें |
|---|---|---|
| 200 OK | Success | Successful GET, PUT |
| 201 Created | Resource बना | Successful POST |
| 401 Unauthorized | Authenticated नहीं | Missing token |
| 404 Not Found | Resource नहीं मिला | Invalid ID |
| 422 | Validation error | Field errors |
Was this answer clear?