Consider a simple REST API simulation where each request is independent. The server does not store any client session data. What will be the output of the following code simulating two requests?
class Server: def __init__(self): self.data = {} def handle_request(self, request): # Each request includes all needed info if request.get('action') == 'store': key = request.get('key') value = request.get('value') # Store data only for this request return f"Stored {key}={value}" elif request.get('action') == 'retrieve': key = request.get('key') # No stored data from previous requests return f"Value for {key}: None" server = Server() # First request stores data print(server.handle_request({'action': 'store', 'key': 'x', 'value': '10'})) # Second request tries to retrieve data print(server.handle_request({'action': 'retrieve', 'key': 'x'}))
Remember that stateless means the server does not keep data between requests.
The server does not save data between requests. So, even though the first request stores a value, the second request cannot retrieve it and returns None.
Choose the statement that correctly explains the statelessness requirement in REST APIs.
Think about how REST APIs handle client data across multiple requests.
Statelessness means the server does not keep client context between requests. Each request must be self-contained.
Examine the code below. Why does this server violate the statelessness requirement?
class Server: def __init__(self): self.sessions = {} def handle_request(self, request): session_id = request.get('session_id') if session_id not in self.sessions: self.sessions[session_id] = {'count': 0} self.sessions[session_id]['count'] += 1 return f"Request count: {self.sessions[session_id]['count']}"
Check if the server keeps any data between requests.
The server stores session data in a dictionary attribute, which means it keeps client state between requests. This breaks statelessness.
What error will this code produce when handling a request missing required data?
def handle_request(request): # Expect 'user_id' in request user_id = request['user_id'] return f"User ID is {user_id}" print(handle_request({'action': 'get_data'}))
What happens if you try to access a dictionary key that does not exist?
Accessing a missing key in a dictionary with square brackets raises a KeyError.
A REST API server is designed to be stateless. It receives three requests in order:
- {"action": "add", "item": "apple"}
- {"action": "add", "item": "banana"}
- {"action": "list"}
The server code is:
class Server:
def __init__(self):
pass
def handle_request(self, request):
if request['action'] == 'add':
# Does not store items
return f"Added {request['item']}"
elif request['action'] == 'list':
# Always returns empty list
return []
server = Server()
print(server.handle_request({"action": "add", "item": "apple"}))
print(server.handle_request({"action": "add", "item": "banana"}))
print(server.handle_request({"action": "list"}))How many items will the server list after these requests?
Think about whether the server keeps any data between requests.
The server does not store any items between requests, so the list action always returns an empty list with 0 items.