0
0
FastAPIframework~20 mins

Async HTTP client calls in FastAPI - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async HTTP Client Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this FastAPI async client call?

Consider this FastAPI endpoint using httpx.AsyncClient to fetch data from an external API asynchronously. What will be the response content when calling /fetch-data?

FastAPI
from fastapi import FastAPI
import httpx

app = FastAPI()

@app.get('/fetch-data')
async def fetch_data():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://httpbin.org/get')
        return response.json()
AA JSON dictionary with keys like 'args', 'headers', 'origin', and 'url' from httpbin.org
BA plain text string with the raw HTML content of https://httpbin.org/get
CA runtime error because httpx.AsyncClient cannot be used inside FastAPI endpoints
DAn empty JSON object {} because the response is not awaited properly
Attempts:
2 left
💡 Hint

Think about what response.json() returns after awaiting the HTTP GET request.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses async HTTP client in FastAPI?

Which code snippet correctly performs an asynchronous HTTP GET request inside a FastAPI endpoint using httpx.AsyncClient?

A
client = httpx.AsyncClient()
response = await client.get('https://example.com')
return response.text
B
async with httpx.AsyncClient() as client:
    response = await client.get('https://example.com')
    return response.text
C
with httpx.AsyncClient() as client:
    response = await client.get('https://example.com')
    return response.text
D
async with httpx.AsyncClient() as client:
    response = client.get('https://example.com')
    return response.text
Attempts:
2 left
💡 Hint

Remember to await async calls and use async context managers properly.

🔧 Debug
advanced
2:00remaining
Why does this FastAPI async client code raise an error?

Given this FastAPI endpoint, why does it raise a RuntimeError: Cannot run the event loop while another loop is running?

FastAPI
from fastapi import FastAPI
import httpx
import asyncio

app = FastAPI()

@app.get('/data')
def get_data():
    async def fetch():
        async with httpx.AsyncClient() as client:
            response = await client.get('https://httpbin.org/get')
            return response.json()
    return asyncio.run(fetch())
ABecause httpx.AsyncClient() must be used synchronously, not asynchronously
BBecause FastAPI endpoints cannot define inner functions
CBecause the inner async function fetch() is missing the await keyword before client.get()
DBecause asyncio.run() cannot be called inside an already running event loop like FastAPI's async server
Attempts:
2 left
💡 Hint

Think about how FastAPI runs async endpoints and event loops.

state_output
advanced
2:00remaining
What is the value of 'result' after this async HTTP call?

In this async function, what will be the value of result after execution?

FastAPI
import httpx

async def fetch_status():
    async with httpx.AsyncClient() as client:
        response = await client.get('https://httpbin.org/status/204')
        return response.status_code

import asyncio
result = asyncio.run(fetch_status())
A204
B200
CNone
DRaises an HTTPError because 204 is no content
Attempts:
2 left
💡 Hint

Check the HTTP status code returned by the URL.

🧠 Conceptual
expert
2:00remaining
Which statement about async HTTP client calls in FastAPI is TRUE?

Choose the correct statement about using async HTTP clients like httpx.AsyncClient in FastAPI applications.

AAsync HTTP clients automatically cache responses to improve performance without extra code
BUsing <code>httpx.AsyncClient</code> requires running FastAPI with multiple worker processes to enable async behavior
CUsing <code>httpx.AsyncClient</code> inside FastAPI endpoints improves concurrency by not blocking the event loop during HTTP calls
DFastAPI endpoints must be synchronous when using async HTTP clients to avoid runtime errors
Attempts:
2 left
💡 Hint

Think about how async calls help with concurrency in web servers.