Skip to content
beginnerPhase ·

Status Codes in REST

Choose the right HTTP status codes for REST API responses.

30m
0 problems
Topic Progress0%

HTTP Status Codes

Status Code Categories

Code Range Category Meaning
1xx Informational Request received, processing
2xx Success Request successfully received and processed
3xx Redirection Further action needed
4xx Client Error Invalid request from client
5xx Server Error Server failed to fulfill request

Common REST Status Codes

Code Name Usage
200 OK Successful GET, PUT, PATCH
201 Created Successful POST
204 No Content Successful DELETE
301 Moved Permanently Resource moved
304 Not Modified Cached response valid
400 Bad Request Invalid input
401 Unauthorized Authentication required
403 Forbidden Authenticated but not authorized
404 Not Found Resource doesn't exist
409 Conflict Resource already exists
422 Unprocessable Entity Validation failed
429 Too Many Requests Rate limit exceeded
500 Internal Server Error Server error
502 Bad Gateway Upstream service error
503 Service Unavailable Service temporarily down

Error Response Format

{
  "error": {
    "code": "VALIDATION_ERROR",
    "message": "Invalid request parameters",
    "details": [
      {
        "field": "email",
        "message": "Must be a valid email address"
      }
    ],
    "timestamp": "2025-01-15T10:30:00Z",
    "traceId": "abc-123-def"
  }
}

REST Best Practices

Principles

  • Stateless communication
  • Client-server separation
  • Cacheable responses
  • Uniform interface

HTTP Methods

  • GET: Read (idempotent, safe)
  • POST: Create
  • PUT: Full update (idempotent)
  • PATCH: Partial update
  • DELETE: Remove (idempotent)

Status Codes

  • 2xx: Success
  • 3xx: Redirection
  • 4xx: Client error
  • 5xx: Server error

Key Points

  • Understanding REST Status Codes is essential for production systems
  • Always consider scalability and maintainability
  • Test thoroughly before deploying to production
  • Monitor performance and set up alerting

Common Patterns

  1. Validation: Always validate input at the boundary
  2. Error Handling: Use structured error responses
  3. Logging: Log key events for debugging
  4. Testing: Unit, integration, and load tests
  5. Documentation: Keep docs updated with code changes

Practice Problems

0/3solved
Implement REST Status Codes

Design and implement a solution for REST Status Codes in a backend system. Consider scalability, error handling, and production readiness.

Solution
// REST Status Codes implementation
// Key aspects: validation, error handling, logging, testing

public class RESTStatusCodes {
    // Production-ready implementation
}
REST Status Codes Edge Cases

Identify and handle edge cases for REST Status Codes. What happens under high load, with invalid input, or during failures?

Solution
// Edge case handling:
// 1. Null/empty input -> validation
// 2. High load -> rate limiting, queuing
// 3. Failures -> retries, circuit breaker
// 4. Concurrent access -> locks, idempotency
REST Status Codes Testing Strategy

Write a testing strategy for REST Status Codes. Include unit tests, integration tests, and performance tests.

Solution
// Test plan:
// - Unit: 80% coverage target
// - Integration: API contracts
// - Performance: latency, throughput
// - Chaos: failure injection

Quiz

1. Which status code should you return after a successful POST that creates a resource?

Question 1 options

2. What does 429 Too Many Requests indicate?

Question 2 options

3. What is the primary purpose of REST Status Codes?

Question 3 options

4. What is a common mistake when implementing REST Status Codes?

Question 4 options

Flashcards

Question

Status code for successful resource creation?

Answer

201 Created

Question

What does 403 Forbidden mean?

Answer

Authenticated but not authorized to access the resource

Question

What is REST Status Codes?

Answer

REST Status Codes is a key concept in backend development.

Question

When to use REST Status Codes?

Answer

Use REST Status Codes when building production systems that require reliability, scalability, and maintainability.

Question

REST Status Codes best practices

Answer

Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.

Revision Notes

Key Takeaways

  • 1.2xx=success, 4xx=client error, 5xx=server error
  • 2.Use 201 for POST creation, 204 for DELETE
  • 3.401=unauthenticated, 403=unauthorized
  • 4.Always include error details in responses

Interview Tips

  • Know common status codes and when to use them
  • Explain the difference between 401 and 403

Cheat Sheet

Status Codes

  • 200: OK, 201: Created, 204: No Content
  • 400: Bad Request, 401: Unauthorized, 403: Forbidden
  • 404: Not Found, 409: Conflict, 422: Validation Error
  • 429: Rate Limited, 500: Server Error
  • 401 vs 403: 401=not logged in, 403=logged in but no permission