0
0
Rest APIprogramming~30 mins

POST for creating resources in Rest API - Mini Project: Build & Apply

Choose your learning style9 modes available
POST for Creating Resources
📖 Scenario: You are building a simple web service that allows users to add new books to a library collection. The service will accept data about books and store them.
🎯 Goal: Create a REST API endpoint that accepts POST requests to add new books to the library collection. You will build the data structure, configure the endpoint, implement the POST logic, and finally test the output.
📋 What You'll Learn
Create a list called library to store book data
Create a configuration variable next_id to assign unique IDs to books
Implement a POST endpoint /books that accepts JSON data with title and author
Add the new book with a unique id to the library list
Return the newly created book data as JSON in the response
💡 Why This Matters
🌍 Real World
Creating REST APIs to add resources is common in web services like online stores, libraries, and social media platforms.
💼 Career
Understanding how to handle POST requests and create resources is essential for backend developers and API designers.
Progress0 / 4 steps
1
Create the initial data structure
Create a list called library that will hold dictionaries representing books. Start with an empty list.
Rest API
Need a hint?

Use library = [] to create an empty list.

2
Add a configuration variable for unique IDs
Create a variable called next_id and set it to 1. This will be used to assign unique IDs to new books.
Rest API
Need a hint?

Use next_id = 1 to start counting book IDs from 1.

3
Implement the POST endpoint logic
Write a function called add_book that takes a dictionary book_data with keys title and author. Inside the function, use the global variable next_id. Create a new dictionary with keys id, title, and author. Assign id the value of next_id. Append this new dictionary to the library list. Increment next_id by 1. Return the new book dictionary.
Rest API
Need a hint?

Remember to use global next_id to modify the variable inside the function.

4
Test the POST endpoint function output
Call the function add_book with the dictionary {'title': '1984', 'author': 'George Orwell'}. Print the returned value.
Rest API
Need a hint?

Use print(add_book({'title': '1984', 'author': 'George Orwell'})) to see the new book added.