URL Patterns
URL Structure
https://api.example.com/v1/products/123?include=reviews&fields=id,name,price
| | | | | | | |
scheme host v resource id query included fields
version
Common URL Patterns
| Method | URL | Description |
|---|---|---|
| GET | /products |
List all products |
| GET | /products/123 |
Get specific product |
| POST | /products |
Create new product |
| PUT | /products/123 |
Replace product |
| PATCH | /products/123 |
Partial update |
| DELETE | /products/123 |
Delete product |
| GET | /products/123/reviews |
Get reviews for product |
| POST | /products/123/reviews |
Add review to product |
Query Parameters
# Pagination
GET /products?page=2&limit=20
# Filtering
GET /products?category=electronics&minPrice=100&maxPrice=500
# Sorting
GET /products?sort=price:asc,name:desc
# Search
GET /products/search?q=wireless+mouse
# Field selection
GET /products?fields=id,name,price
# Include related
GET /products/123?include=reviews,category
URL Best Practices
| Do | Don't |
|---|---|
/users/123 |
/getUser/123 |
/order-items |
/orderItems |
/products?sort=price:asc |
/products?sortBy=price&order=asc |
| Use query params for filtering | Use path for every filter |
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 RESTful URL Design is essential for production systems
- Always consider scalability and maintainability
- Test thoroughly before deploying to production
- Monitor performance and set up alerting
Common Patterns
- Validation: Always validate input at the boundary
- Error Handling: Use structured error responses
- Logging: Log key events for debugging
- Testing: Unit, integration, and load tests
- Documentation: Keep docs updated with code changes
Practice Problems
Design and implement a solution for RESTful URL Design in a backend system. Consider scalability, error handling, and production readiness.
Solution
// RESTful URL Design implementation
// Key aspects: validation, error handling, logging, testing
public class RESTfulURLDesign {
// Production-ready implementation
} Identify and handle edge cases for RESTful URL Design. 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 Write a testing strategy for RESTful URL Design. 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. How should filtering be done in REST?
2. What is the correct URL for getting reviews of product 123?
3. What is a common mistake when implementing RESTful URL Design?
Flashcards
Question
How to handle filtering in REST?
Click to reveal answer
Answer
Query parameters: /products?category=electronics&minPrice=100
Question
Should URLs use camelCase or hyphens?
Click to reveal answer
Answer
Hyphens: /order-items not /orderItems
Question
RESTful URL Design best practices
Click to reveal answer
Answer
Follow SOLID principles, write clean code, test thoroughly, document decisions, and monitor in production.
Revision Notes
Key Takeaways
- 1. Use query parameters for filtering, sorting, pagination
- 2. Use hyphens for multi-word URLs, plural nouns
- 3. Keep URLs clean and self-descriptive
- 4. Version APIs in the URL path (/v1/products)
Interview Tips
- • Design URLs for a given scenario
- • Know when to use query params vs path segments
Cheat Sheet
URL Design
- Plural nouns:
/products,/users - Hyphens:
/order-items(not camelCase) - Filtering: Query params
?category=electronics - Pagination:
?page=2&limit=20 - Versioning:
/v1/products