Request body structure in Rest API - Time & Space Complexity
When working with request bodies in APIs, it's important to understand how processing time changes as the size of the data grows.
We want to know how the time to handle the request body changes when the input gets bigger.
Analyze the time complexity of the following code snippet.
POST /users HTTP/1.1
Content-Type: application/json
{
"users": [
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25},
...
]
}
This code snippet represents a POST request sending a JSON body with a list of user objects to the server.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The server reads and processes each user object in the "users" array.
- How many times: Once for each user in the array, so the number of users determines the repetitions.
As the number of users in the request body grows, the processing time grows in a similar way.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Processes 10 user objects |
| 100 | Processes 100 user objects |
| 1000 | Processes 1000 user objects |
Pattern observation: The time to process grows directly with the number of users; doubling users roughly doubles the work.
Time Complexity: O(n)
This means the processing time grows in a straight line with the number of items in the request body.
[X] Wrong: "Processing the request body always takes the same time no matter how many items it has."
[OK] Correct: The server must handle each item, so more items mean more work and more time.
Understanding how request body size affects processing time helps you design efficient APIs and handle data smartly in real projects.
"What if the request body contains nested arrays of users instead of a single array? How would the time complexity change?"
Practice
request body in a REST API POST request?Solution
Step 1: Understand the role of request body
The request body contains the data sent by the client to the server, usually in POST or PUT requests.Step 2: Differentiate from other parts of the request
The URL specifies the endpoint, the method defines the action, and the status code is part of the response, not the request body.Final Answer:
To send data from the client to the server -> Option BQuick Check:
Request body = data sent to server [OK]
- Confusing request body with URL or headers
- Thinking request body sets HTTP method
- Mixing request body with response status
Solution
Step 1: Identify the header for JSON data
The headerContent-Type: application/jsontells the server the body contains JSON formatted data.Step 2: Compare with other content types
text/plainis plain text,multipart/form-datais for file uploads, andapplication/xmlis for XML data.Final Answer:
Content-Type: application/json -> Option CQuick Check:
JSON header = application/json [OK]
- Using text/plain for JSON data
- Forgetting to set Content-Type header
- Using multipart/form-data without files
{
"name": "Alice",
"age": 30
}What will the server receive as the value of
age?Solution
Step 1: Analyze the JSON data types
The value forageis written as30without quotes, so it is a number, not a string.Step 2: Understand server parsing
The server parses JSON and keeps numbers as numbers, soagewill be the number 30.Final Answer:
30 (number) -> Option DQuick Check:
Unquoted numbers in JSON = number type [OK]
- Assuming all JSON values are strings
- Confusing number and string types
- Ignoring JSON parsing rules
{
"username": "bob",
"password": "1234"
}But the server returns a 400 Bad Request error. What is a likely cause?
Solution
Step 1: Check request headers and format
A 400 Bad Request often means the server cannot parse the body, commonly due to missing or incorrect Content-Type header.Step 2: Evaluate other options
Username length or password content usually cause validation errors, not 400 errors. GET requests do not have bodies, but the question implies a body was sent.Final Answer:
Missing Content-Type header or wrong format -> Option AQuick Check:
400 error often = bad or missing Content-Type [OK]
- Ignoring Content-Type header
- Assuming 400 means validation error
- Confusing GET with POST request body
{
"name": "Eve",
"address": {
"street": "123 Main St",
"city": "Townsville"
}
}Which Content-Type header and body format should you use to ensure the server correctly understands this nested data?
Solution
Step 1: Identify correct header for nested JSON
Nested JSON requiresapplication/jsonContent-Type so the server parses the structure correctly.Step 2: Evaluate other formats
text/plaintreats body as plain text,application/x-www-form-urlencodeddoes not support nested objects well, andmultipart/form-datais for files, not nested JSON.Final Answer:
Content-Type: application/json with the JSON body as shown -> Option AQuick Check:
Nested JSON needs application/json header [OK]
- Using form-urlencoded for nested JSON
- Sending JSON as plain text without header
- Uploading JSON as file instead of body
Ready to prove it?
Pass the test and this pattern moves unknown
