POST for creating resources in Rest API - Time & Space Complexity
When we send a POST request to create a resource, the server does some work to save it.
We want to know how the time it takes grows as we create more or bigger resources.
Analyze the time complexity of the following code snippet.
POST /items
Request body: { "name": "ItemName", "details": {...} }
// Server side pseudocode
function createItem(data) {
database.insert(data);
return { status: 201, message: "Created" };
}
This code receives data and inserts it into a database to create a new item.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Inserting one item into the database.
- How many times: Once per POST request.
Each POST request adds one item, so the work is about the same each time.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 1 insert operation |
| 100 | 1 insert operation |
| 1000 | 1 insert operation |
Pattern observation: The time remains constant regardless of input size.
Time Complexity: O(1)
This means each POST request takes about the same time, no matter how many items exist.
[X] Wrong: "Creating more items makes each POST slower because the database is bigger."
[OK] Correct: Usually, inserting one item is a fixed cost; the database handles growth internally without slowing each insert noticeably.
Understanding how POST requests scale helps you design APIs that stay fast as users add more data.
"What if the server had to check all existing items before inserting? How would the time complexity change?"
Practice
POST method in REST APIs?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 AQuick Check:
POST = Create resource [OK]
- Confusing POST with GET which only fetches data
- Thinking POST updates existing resources fully
- Assuming POST deletes resources
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 BQuick Check:
POST data = request body JSON [OK]
- Putting data in URL query instead of body
- Trying to send data via headers
- Using cookies to send creation data
import requests
url = 'https://api.example.com/items'
data = {'name': 'Book', 'price': 12.99}
response = requests.post(url, json=data)
print(response.status_code)Solution
Step 1: Understand POST success codes
When a resource is created, the server usually returns HTTP status 201 Created.Step 2: Analyze the code behavior
The code sends a POST request with JSON data; if successful, response.status_code will be 201.Final Answer:
201 -> Option CQuick Check:
POST success = 201 Created [OK]
- Confusing 200 OK with 201 Created
- Assuming 404 means success
- Thinking 500 means success
import requests
url = 'https://api.example.com/users'
data = {'username': 'alice'}
response = requests.post(url, data=data)
print(response.status_code)Solution
Step 1: Check data sending method
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 AQuick Check:
Send JSON with json= parameter [OK]
- Sending form data instead of JSON
- Assuming URL is wrong without checking
- Thinking POST can't send data
- Ignoring HTTPS requirement
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 DQuick Check:
JSON object with keys and values = {"title": "My Day", "content": "It was sunny."} [OK]
- Using JSON arrays instead of objects
- Swapping keys and values
- Sending URL-encoded string instead of JSON
Ready to prove it?
Pass the test and this pattern moves unknown
