What if your URLs could speak clearly without shouting commands?
Why Avoiding verbs in URLs in Rest API? - Purpose & Use Cases
Imagine you are building a website where users can manage their tasks. You decide to name your web addresses like /createTask or /deleteTask to perform actions.
This approach quickly becomes confusing and inconsistent. It mixes actions with resource names, making URLs hard to remember and maintain. Also, it breaks the natural way web browsers and servers expect URLs to work.
By avoiding verbs in URLs and focusing on nouns (resources), you create clear, consistent addresses like /tasks. Actions are then handled by HTTP methods like GET, POST, DELETE, making your API easier to understand and use.
POST /createTask
GET /deleteTask?id=123POST /tasks
DELETE /tasks/123This approach enables clean, predictable URLs that work naturally with web standards and make your API easy to use and scale.
Think of an online store: instead of /buyProduct, you use /products with POST to add to cart, and DELETE to remove items, making the system intuitive and consistent.
Manual URLs with verbs mix actions and resources, causing confusion.
Using nouns in URLs and HTTP methods for actions keeps APIs clean.
This leads to easier maintenance and better user experience.
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
