0
0
Rest APIprogramming~30 mins

200 OK and 201 Created in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
Understanding 200 OK and 201 Created in REST API
📖 Scenario: You are building a simple REST API for managing a list of books in a library. The API should respond correctly when a client requests the list of books or adds a new book.
🎯 Goal: Learn how to send proper HTTP status codes 200 OK when returning data and 201 Created when a new resource is created.
📋 What You'll Learn
Create a list of books as initial data
Add a configuration variable for the new book to add
Write the core logic to handle GET and POST requests with correct status codes
Print the HTTP status code and response message
💡 Why This Matters
🌍 Real World
APIs use status codes like 200 and 201 to tell clients if their requests succeeded or if new data was created.
💼 Career
Understanding HTTP status codes is essential for backend developers and anyone working with web services.
Progress0 / 4 steps
1
Create initial book list
Create a list called books with these exact dictionaries: {'id': 1, 'title': '1984'} and {'id': 2, 'title': 'Brave New World'}.
Rest API
Need a hint?

Use a list with two dictionaries inside, each dictionary has keys 'id' and 'title'.

2
Add new book data
Create a dictionary called new_book with 'id': 3 and 'title': 'Fahrenheit 451'.
Rest API
Need a hint?

Use a dictionary with keys 'id' and 'title' exactly as shown.

3
Implement GET and POST logic
Write a function called handle_request that takes a parameter method. If method is 'GET', return a tuple (200, books). If method is 'POST', append new_book to books and return (201, new_book).
Rest API
Need a hint?

Use if and elif to check the method, then return the correct status code and data.

4
Print the response for GET and POST
Call handle_request with 'GET' and print the status code and data. Then call it with 'POST' and print the status code and data.
Rest API
Need a hint?

Call the function twice with 'GET' and 'POST' and print the results using f-strings.