Challenge - 5 Problems
Request Timing Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this FastAPI middleware output?
Consider this FastAPI middleware that measures request processing time and adds it to the response headers. What will be the value of the header 'X-Process-Time' in the response?
FastAPI
from fastapi import FastAPI, Request import time app = FastAPI() @app.middleware('http') async def add_process_time_header(request: Request, call_next): start_time = time.time() response = await call_next(request) process_time = time.time() - start_time response.headers['X-Process-Time'] = str(process_time) return response
Attempts:
2 left
💡 Hint
Think about what time.time() measures and how the difference is used.
✗ Incorrect
The middleware records the time before and after processing the request. The difference is the processing time in seconds, which is added as a string to the 'X-Process-Time' header.
📝 Syntax
intermediate2:00remaining
Identify the syntax error in this FastAPI middleware
Which option contains a syntax error that will prevent this FastAPI middleware from running?
FastAPI
from fastapi import FastAPI, Request import time app = FastAPI() @app.middleware('http') async def timing_middleware(request: Request, call_next): start = time.time() response = await call_next(request) duration = time.time() - start response.headers['X-Duration'] = str(duration) return response
Attempts:
2 left
💡 Hint
Remember that 'await' can only be used inside async functions.
✗ Incorrect
Removing 'async' causes a syntax error because 'await' is used inside the function. The function must be async to use await.
🔧 Debug
advanced2:00remaining
Why does this middleware not add the timing header?
This middleware is intended to add a header 'X-Time' with the request duration, but the header is missing in responses. What is the cause?
FastAPI
from fastapi import FastAPI, Request import time app = FastAPI() @app.middleware('http') async def timing(request: Request, call_next): start = time.time() response = call_next(request) # Missing await duration = time.time() - start response.headers['X-Time'] = str(duration) return response
Attempts:
2 left
💡 Hint
Check how async functions and await work with call_next.
✗ Incorrect
call_next returns a coroutine that must be awaited to get the response object. Without await, response is not a response object, so headers cannot be set.
❓ state_output
advanced2:00remaining
What is the output of this middleware with nested calls?
Given this middleware that adds timing and a route that calls another route internally, what will be the value of 'X-Process-Time' in the final response?
FastAPI
from fastapi import FastAPI, Request import time app = FastAPI() @app.middleware('http') async def timer(request: Request, call_next): start = time.time() response = await call_next(request) response.headers['X-Process-Time'] = str(time.time() - start) return response @app.get('/inner') async def inner(): time.sleep(0.1) return {'message': 'inner'} @app.get('/outer') async def outer(): from fastapi.testclient import TestClient client = TestClient(app) res = client.get('/inner') return {'inner_response': res.json()}
Attempts:
2 left
💡 Hint
Think about how TestClient calls are separate HTTP requests.
✗ Incorrect
The middleware measures time per HTTP request. The internal TestClient call to '/inner' is a separate request, so the timing header on '/outer' only measures its own processing time.
🧠 Conceptual
expert3:00remaining
What is the best way to ensure accurate request timing in FastAPI middleware?
Which approach ensures the most accurate measurement of request processing time in FastAPI middleware?
Attempts:
2 left
💡 Hint
Middleware wraps the entire request processing.
✗ Incorrect
Measuring time before and after awaiting call_next captures the full request processing time including route handler and other middleware.