Good URI Design Which of the following rules should be followed to design a good URI? Pick ONE OR MORE options URIs should never be changed URIs must be constructed by the client URIs should be short in length URIs should be case-sensitive HTTP verbs should be used instead of operation names in URIs Use spaces when designing a URI Redirection must be used if a change in URI is required
The correct answer and explanation is:
Correct Answers:
- URIs should be short in length
- HTTP verbs should be used instead of operation names in URIs
- Redirection must be used if a change in URI is required
Explanation
A Uniform Resource Identifier (URI) is a key component of web APIs and web resources. A well-designed URI ensures usability, clarity, and longevity. Below is an explanation of why certain URI design principles should be followed:
1. URIs Should Be Short in Length
Short URIs are easier to read, remember, and type. Long, complex URIs increase the risk of errors and reduce usability. A well-structured, concise URI enhances user experience and improves SEO for websites. For example:
✅ Good: https://api.example.com/products/123
❌ Bad: https://api.example.com/store/products/category/electronics/brand/samsung/item/12345/details
2. HTTP Verbs Should Be Used Instead of Operation Names in URIs
RESTful APIs should use HTTP methods (GET, POST, PUT, DELETE) rather than embedding operations in URIs. This makes the API more intuitive and adheres to REST principles.
✅ Good: GET /users/123 (to fetch user details)
❌ Bad: GET /getUserById/123
3. Redirection Must Be Used If a Change in URI is Required
URIs should be stable over time, but if a change is unavoidable, HTTP 301 (Moved Permanently) or HTTP 302 (Found/Redirect) must be used to ensure proper redirection. This helps maintain backward compatibility and avoids breaking existing links.
✅ Good: Redirecting /old-url → /new-url using HTTP 301
❌ Bad: Simply removing the old URI, causing a 404 error
Incorrect Options & Why They Are Wrong
- “URIs should never be changed” → While stability is important, sometimes URIs need to be updated for better clarity or organization. Redirection should be used in such cases.
- “URIs must be constructed by the client” → Clients should not create URIs dynamically; they should use predefined endpoints from the API documentation.
- “URIs should be case-sensitive” → While technically possible, it’s not recommended as it can cause confusion (
/Productvs/product). Best practice is to use lowercase. - “Use spaces when designing a URI” → Spaces are invalid in URIs and require encoding (
%20), which makes them harder to read. Instead, use hyphens (-) or underscores (_).
By following these best practices, URIs remain readable, scalable, and maintainable. 🚀