Avoiding verbs in URLs in Rest API - Time & Space Complexity
When designing REST APIs, the way URLs are structured affects how the server handles requests.
We want to understand how the choice of URL style impacts the number of operations the server performs.
Analyze the time complexity of handling requests with URLs that avoid verbs.
GET /users/123
POST /users
PUT /users/123
DELETE /users/123
This code snippet shows typical REST API URLs using nouns and HTTP methods instead of verbs in the URL path.
Look at what the server does when it receives these requests.
- Primary operation: Searching or updating user data by ID.
- How many times: Once per request, the server looks up the user or modifies data.
The server work depends on how many users exist and how it finds them.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 users | About 10 checks to find one user |
| 100 users | About 100 checks |
| 1000 users | About 1000 checks |
Pattern observation: The time to find or update a user grows as the number of users grows.
Time Complexity: O(n)
This means the server work grows linearly with the number of users when searching or updating.
[X] Wrong: "Using verbs in URLs makes the server faster because it knows the action upfront."
[OK] Correct: The server still needs to find or update data; the URL style does not reduce the search or update work.
Understanding how URL design affects server work helps you build clear and efficient APIs, a skill valued in many projects.
"What if the server used an index to find users instead of searching all? How would the time complexity change?"
Practice
Solution
Step 1: Understand URL naming conventions
REST APIs should use nouns to represent resources, not verbs. URLs like/users/123represent a user resource with ID 123.Step 2: Identify verbs in URLs
URLs like/getUser/123,/createUser, and/deleteUser/123contain verbs (get, create, delete), which is against best practice.Final Answer:
/users/123-> Option DQuick Check:
Use nouns in URLs =/users/123[OK]
- Including verbs like 'get' or 'delete' in the URL
- Using action words instead of resource names
- Confusing HTTP methods with URL paths
/orders/456 to update an existing order?Solution
Step 1: Match HTTP methods to actions
GET retrieves data, POST creates new data, PUT updates existing data, DELETE removes data.Step 2: Identify update action
To update an existing order at/orders/456, the correct HTTP method is PUT.Final Answer:
PUT -> Option AQuick Check:
Update = PUT [OK]
- Using POST to update instead of create
- Using GET to modify data
- Confusing PUT and DELETE methods
DELETE /products/789, what is the expected action?Solution
Step 1: Understand HTTP DELETE method
DELETE is used to remove a resource identified by the URL.Step 2: Apply to given URL
The URL/products/789identifies product 789, so DELETE removes it.Final Answer:
Delete product with ID 789 -> Option AQuick Check:
DELETE removes resource = Delete product with ID 789 [OK]
- Thinking DELETE retrieves data
- Confusing DELETE with POST or PUT
- Assuming URL contains action verbs
POST /updateUser/123Solution
Step 1: Check URL naming conventions
URLs should use nouns to represent resources, not verbs like 'update'. The verb should be expressed by the HTTP method.Step 2: Analyze the given URL
The URL/updateUser/123contains the verb 'update', which violates REST best practices.Final Answer:
URL contains a verb 'update', which should be avoided -> Option CQuick Check:
Verbs in URL = URL contains a verb 'update', which should be avoided [OK]
- Putting verbs like 'update' in URLs
- Misusing HTTP methods
- Ignoring resource identification in URLs
Solution
Step 1: Identify correct URL structure
The URL should use a noun representing the resource, here/posts/321, without verbs like 'delete'.Step 2: Match HTTP method to action
To delete a resource, use the DELETE method with the resource URL.Final Answer:
DELETE /posts/321 -> Option BQuick Check:
Delete resource = DELETE + noun URL [OK]
- Including verbs like 'delete' in the URL
- Using GET or POST for delete actions
- Placing action words after resource ID
Ready to prove it?
Pass the test and this pattern moves unknown
