Bird
Raised Fist0
Rest APIprogramming~5 mins

Resource-based design thinking in Rest API

Choose your learning style10 modes available
Introduction

Resource-based design thinking helps you organize your API around things (resources) like users or products. It makes APIs easy to understand and use.

When building a web API to manage data like users, orders, or products.
When you want your API to be simple and follow common web standards.
When you want to clearly separate different parts of your system by their data.
When you want to use HTTP methods (GET, POST, PUT, DELETE) in a meaningful way.
When you want your API to be easy for other developers to learn and use.
Syntax
Rest API
HTTP_METHOD /resource_name/{resource_id}

Example:
GET /users/123
POST /products
PUT /orders/456
DELETE /comments/789

Use 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.

Examples
This gets all books from the server.
Rest API
GET /books

Returns a list of books.
This adds a new book to the collection.
Rest API
POST /books

Creates a new book with data sent in the request body.
This changes details of the book number 10.
Rest API
PUT /books/10

Updates the book with ID 10.
This removes the book number 10 from the collection.
Rest API
DELETE /books/10

Deletes the book with ID 10.
Sample Program

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.

Rest API
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)
OutputSuccess
Important Notes

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.

Summary

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

(1/5)
1. What is the main idea behind resource-based design thinking in REST APIs?
easy
A. Focusing only on database tables
B. Using only POST requests for all actions
C. Designing APIs without any structure
D. Organizing APIs around things like users or products

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    Organizing APIs around things like users or products -> Option D
  4. Quick Check:

    Resource-based design = Organizing APIs around resources [OK]
Hint: Think of APIs as managing 'things' like users or products [OK]
Common Mistakes:
  • Confusing resource-based design with database design
  • Thinking all requests must be POST
  • Ignoring the concept of resources
2. Which HTTP method is typically used to update an existing resource in a REST API?
easy
A. PUT
B. POST
C. GET
D. DELETE

Solution

  1. Step 1: Recall HTTP methods and their common uses

    GET retrieves data, POST creates new data, PUT updates existing data, DELETE removes data.
  2. Step 2: Identify the method for updating

    PUT is the standard method used to update an existing resource in REST APIs.
  3. Final Answer:

    PUT -> Option A
  4. Quick Check:

    Update resource = PUT [OK]
Hint: PUT means replace or update a resource fully [OK]
Common Mistakes:
  • Using POST to update instead of create
  • Confusing GET with update
  • Using DELETE for updating
3. Given this REST API call: DELETE /products/123, what is the expected result?
medium
A. Retrieve product with ID 123
B. Create a new product with ID 123
C. Delete product with ID 123
D. Update product with ID 123

Solution

  1. Step 1: Analyze the HTTP method and URL

    DELETE method means remove. The URL targets the product resource with ID 123.
  2. Step 2: Determine the action

    DELETE /products/123 means delete the product with ID 123 from the system.
  3. Final Answer:

    Delete product with ID 123 -> Option C
  4. Quick Check:

    DELETE + resource ID = delete that resource [OK]
Hint: DELETE + resource ID means remove that resource [OK]
Common Mistakes:
  • Thinking DELETE retrieves data
  • Confusing DELETE with POST or PUT
  • Ignoring the resource ID in URL
4. You see this API request: POST /users/45. What is wrong with this request in resource-based design?
medium
A. The URL should be /users without ID for creating
B. POST cannot be used to create resources
C. The HTTP method should be PUT
D. POST must include a resource ID in the URL

Solution

  1. 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.
  2. 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.
  3. Final Answer:

    The URL should be /users without ID for creating -> Option A
  4. Quick Check:

    POST to collection URL without ID = correct [OK]
Hint: POST creates; URL should be collection without ID [OK]
Common Mistakes:
  • Using POST with resource ID in URL
  • Thinking POST updates existing resource
  • Confusing POST and PUT URLs
5. You want to design an API to manage orders and their items. Which URL and HTTP method combination best follows resource-based design to update the quantity of item 7 in order 42?
hard
A. POST /orders/42/items with item ID and quantity in body
B. PUT /orders/42/items/7 with updated quantity in body
C. DELETE /orders/42/items/7 to update quantity
D. GET /orders/42/items/7 with quantity in query

Solution

  1. Step 1: Identify the resource and action

    The resource is the specific item (ID 7) inside order 42. Updating quantity means modifying this resource.
  2. 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.
  3. Final Answer:

    PUT /orders/42/items/7 with updated quantity in body -> Option B
  4. Quick Check:

    Update nested resource = PUT to specific item URL [OK]
Hint: Use PUT on specific nested resource URL to update it [OK]
Common Mistakes:
  • Using POST to update existing item
  • Using DELETE to update data
  • Using GET with query to change data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard