The client.get() is an async method and must be awaited to get the response.
Step 2: Identify missing await
Without await, the code returns a coroutine object, causing errors when accessing status_code.
Final Answer:
Missing await before client.get() call -> Option B
Quick Check:
Async calls require await = A [OK]
Hint: Always await async client.get() calls [OK]
Common Mistakes:
Not awaiting async HTTP calls
Using sync client in async function
Forgetting async keyword on function
5. You want to fetch data from two different URLs concurrently in a FastAPI async endpoint. Which code correctly achieves this using httpx.AsyncClient?
hard
A. async with httpx.AsyncClient() as client:
r1 = await client.get(url1)
r2 = await client.get(url2)
return {'r1': r1.status_code, 'r2': r2.status_code}
B. async with httpx.AsyncClient() as client:
r1, r2 = await client.get(url1), await client.get(url2)
return {'r1': r1.status_code, 'r2': r2.status_code}
C. async with httpx.AsyncClient() as client:
import asyncio
r1, r2 = await asyncio.gather(client.get(url1), client.get(url2))
return {'r1': r1.status_code, 'r2': r2.status_code}