📖 Scenario: You work for a company that manages a list of products. You need to create a simple REST API to handle bulk import and export of product data in JSON format.This will help the company quickly add many products at once and also download all products easily.
🎯 Goal: Build a REST API with two endpoints:/import to accept a bulk list of products and store them./export to return all stored products as JSON.You will create the data storage, configure the API, write the core logic for import and export, and finally test the output.
📋 What You'll Learn
Create an in-memory list called products to store product dictionaries
Create a configuration variable max_import_size to limit bulk import size
Write a POST endpoint /import that accepts JSON list of products and adds them to products if size is within max_import_size
Write a GET endpoint /export that returns all stored products as JSON
💡 Why This Matters
🌍 Real World
Bulk import and export APIs are common in inventory management, e-commerce, and data migration tasks where many items need to be added or retrieved efficiently.
💼 Career
Understanding how to build bulk data endpoints is valuable for backend developers working with REST APIs, enabling efficient data handling and integration with other systems.
Progress0 / 4 steps
1
Create the initial data storage
Create an empty list called products to store product dictionaries.
Rest API
Hint
Use a list to hold multiple product items.
2
Add a configuration variable
Create a variable called max_import_size and set it to 5 to limit the number of products imported at once.
Rest API
Hint
This variable will help control how many products can be imported in one request.
3
Write the bulk import endpoint
Using Flask, create a POST endpoint /import that reads a JSON list of products from the request body. If the list length is less than or equal to max_import_size, add all products to the products list and return JSON {"status": "success"}. Otherwise, return JSON {"status": "error", "message": "Too many products"} with status code 400.
Rest API
Hint
Use request.get_json() to get the JSON data sent by the client.
Check the length of the list before adding.
4
Write the bulk export endpoint and test output
Create a GET endpoint /export that returns all products stored in the products list as JSON. Then, print the output of calling app.test_client().get('/export').get_json() to show the exported products.
Rest API
Hint
Return the products list as JSON in the /export endpoint.
Use Flask's test client to simulate requests and print the exported data.
Practice
(1/5)
1. What HTTP method is commonly used to perform a bulk import of records in a REST API?
easy
A. PUT
B. POST
C. DELETE
D. GET
Solution
Step 1: Understand bulk import action
Bulk import means sending many new records to the server at once.
Step 2: Identify HTTP method for creating resources
POST is used to send data to create new records in REST APIs.
Final Answer:
POST -> Option B
Quick Check:
Bulk import uses POST [OK]
Hint: Bulk import sends data with POST method [OK]
Common Mistakes:
Choosing GET which is for fetching data
Using DELETE which removes data
Confusing PUT with bulk import
2. Which of the following is the correct JSON format to bulk import two user records in a REST API POST request?
easy
A. [{"name": "Alice"}, {"name": "Bob"}]
B. {"name": "Alice", "name": "Bob"}
C. {"users": "Alice, Bob"}
D. ["Alice", "Bob"]
Solution
Step 1: Recognize bulk import JSON structure
Bulk import expects a list (array) of objects, each representing a record.
Step 2: Check each option format
[{"name": "Alice"}, {"name": "Bob"}] is a JSON array with two objects, each with a "name" key. Others are invalid or wrong format.
Final Answer:
[{"name": "Alice"}, {"name": "Bob"}] -> Option A
Quick Check:
Bulk import uses JSON array of objects [OK]
Hint: Bulk import JSON is a list of objects [OK]
Common Mistakes:
Using a single object with duplicate keys
Sending a string instead of objects
Using a simple list of strings without keys
3. Given this REST API response after a bulk export request: