📖 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
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
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
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
Hint
Use print(add_book({'title': '1984', 'author': 'George Orwell'})) to see the new book added.
Practice
(1/5)
1. What is the primary purpose of the POST method in REST APIs?
easy
A. To create a new resource on the server
B. To retrieve data from the server
C. To update an existing resource completely
D. To delete a resource from the server
Solution
Step 1: Understand HTTP methods
HTTP methods have specific roles: GET retrieves, POST creates, PUT updates, DELETE removes.
Step 2: Identify POST's role
POST sends data to the server to create a new resource, unlike GET which only fetches data.
Final Answer:
To create a new resource on the server -> Option A
Quick Check:
POST = Create resource [OK]
Hint: POST is for sending data to create something new [OK]
Common Mistakes:
Confusing POST with GET which only fetches data
Thinking POST updates existing resources fully
Assuming POST deletes resources
2. Which of the following is the correct way to send data in a POST request to create a resource?
easy
A. Include data in the URL query parameters
B. Send data in the request body as JSON
C. Send data as HTTP headers
D. Send data as a cookie
Solution
Step 1: Identify where POST data goes
POST requests send data in the request body, not in URL or headers.
Step 2: Recognize common data format
JSON is the standard format for sending structured data in the body for REST APIs.
Final Answer:
Send data in the request body as JSON -> Option B
Quick Check:
POST data = request body JSON [OK]
Hint: POST data goes in the body, not URL or headers [OK]
Common Mistakes:
Putting data in URL query instead of body
Trying to send data via headers
Using cookies to send creation data
3. Given this Python code using the requests library, what will be the HTTP status code if the resource is created successfully?
Using data=data sends form-encoded data, but API expects JSON for POST creation.
Step 2: Fix data format
Use json=data in requests.post to send JSON body, which the server accepts.
Final Answer:
The data should be sent as JSON, not form data -> Option A
Quick Check:
Send JSON with json= parameter [OK]
Hint: Use json= not data= to send JSON body [OK]
Common Mistakes:
Sending form data instead of JSON
Assuming URL is wrong without checking
Thinking POST can't send data
Ignoring HTTPS requirement
5. You want to create a new blog post resource with title and content using a POST request. Which of these JSON payloads correctly represents the data to send in the request body?
hard
A. ["title", "My Day", "content", "It was sunny."]
B. "title=My Day&content=It was sunny."
C. {"My Day": "title", "It was sunny.": "content"}
D. {"title": "My Day", "content": "It was sunny."}
Solution
Step 1: Understand JSON object structure
JSON objects use key-value pairs with keys as strings and values as data.
Step 2: Identify correct JSON format
{"title": "My Day", "content": "It was sunny."} correctly uses keys "title" and "content" with their string values in an object.
Final Answer:
{"title": "My Day", "content": "It was sunny."} -> Option D
Quick Check:
JSON object with keys and values = {"title": "My Day", "content": "It was sunny."} [OK]
Hint: JSON data must be key-value pairs inside braces {} [OK]
Common Mistakes:
Using JSON arrays instead of objects
Swapping keys and values
Sending URL-encoded string instead of JSON
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.