Resource-based design thinking helps you organize your API around things (resources) like users or products. It makes APIs easy to understand and use.
Resource-based design thinking in Rest API
HTTP_METHOD /resource_name/{resource_id}
Example:
GET /users/123
POST /products
PUT /orders/456
DELETE /comments/789Use nouns (resource names) in URLs, not verbs.
Use HTTP methods to describe actions: GET to read, POST to create, PUT to update, DELETE to remove.
GET /books
Returns a list of books.POST /books Creates a new book with data sent in the request body.
PUT /books/10 Updates the book with ID 10.
DELETE /books/10 Deletes the book with ID 10.
This simple API manages a list of books as a resource. You can get all books, add a new book, update a book by ID, or delete a book by ID. It uses resource-based design thinking by treating books as resources with URLs and HTTP methods.
from flask import Flask, jsonify, request app = Flask(__name__) books = [ {"id": 1, "title": "Learn Python"}, {"id": 2, "title": "REST API Basics"} ] @app.route('/books', methods=['GET']) def get_books(): return jsonify(books) @app.route('/books', methods=['POST']) def add_book(): new_book = request.get_json() books.append(new_book) return jsonify(new_book), 201 @app.route('/books/<int:book_id>', methods=['PUT']) def update_book(book_id): updated_data = request.get_json() for book in books: if book['id'] == book_id: book.update(updated_data) return jsonify(book) return jsonify({'error': 'Book not found'}), 404 @app.route('/books/<int:book_id>', methods=['DELETE']) def delete_book(book_id): for book in books: if book['id'] == book_id: books.remove(book) return jsonify({'message': 'Book deleted'}) return jsonify({'error': 'Book not found'}), 404 if __name__ == '__main__': app.run(debug=True)
Always use clear and consistent resource names in your URLs.
Use HTTP status codes to tell if requests succeeded or failed.
Keep URLs simple and avoid actions in the path; use HTTP methods instead.
Resource-based design thinking organizes APIs around things (resources) like users or products.
Use HTTP methods (GET, POST, PUT, DELETE) to act on resources.
This approach makes APIs easier to understand and use.
Practice
resource-based design thinking in REST APIs?Solution
Step 1: Understand the concept of resource-based design
Resource-based design means APIs are built around resources, which are things like users, products, or orders.Step 2: Compare options to the concept
Only Organizing APIs around things like users or products correctly describes organizing APIs around things (resources). Other options describe incorrect or unrelated ideas.Final Answer:
Organizing APIs around things like users or products -> Option DQuick Check:
Resource-based design = Organizing APIs around resources [OK]
- Confusing resource-based design with database design
- Thinking all requests must be POST
- Ignoring the concept of resources
Solution
Step 1: Recall HTTP methods and their common uses
GET retrieves data, POST creates new data, PUT updates existing data, DELETE removes data.Step 2: Identify the method for updating
PUT is the standard method used to update an existing resource in REST APIs.Final Answer:
PUT -> Option AQuick Check:
Update resource = PUT [OK]
- Using POST to update instead of create
- Confusing GET with update
- Using DELETE for updating
DELETE /products/123, what is the expected result?Solution
Step 1: Analyze the HTTP method and URL
DELETE method means remove. The URL targets the product resource with ID 123.Step 2: Determine the action
DELETE /products/123 means delete the product with ID 123 from the system.Final Answer:
Delete product with ID 123 -> Option CQuick Check:
DELETE + resource ID = delete that resource [OK]
- Thinking DELETE retrieves data
- Confusing DELETE with POST or PUT
- Ignoring the resource ID in URL
POST /users/45. What is wrong with this request in resource-based design?Solution
Step 1: Understand POST usage in REST
POST is used to create new resources. The URL should point to the collection, not a specific resource ID.Step 2: Analyze the request URL
Including an ID (like /users/45) with POST is incorrect because the server assigns IDs when creating new resources.Final Answer:
The URL should be/userswithout ID for creating -> Option AQuick Check:
POST to collection URL without ID = correct [OK]
- Using POST with resource ID in URL
- Thinking POST updates existing resource
- Confusing POST and PUT URLs
Solution
Step 1: Identify the resource and action
The resource is the specific item (ID 7) inside order 42. Updating quantity means modifying this resource.Step 2: Choose correct HTTP method and URL
PUT is used to update existing resources. The URL should point to the specific item resource: /orders/42/items/7.Final Answer:
PUT /orders/42/items/7 with updated quantity in body -> Option BQuick Check:
Update nested resource = PUT to specific item URL [OK]
- Using POST to update existing item
- Using DELETE to update data
- Using GET with query to change data
Ready to prove it?
Pass the test and this pattern moves unknown
