Complete the code to import the async HTTP client from httpx.
from [1] import AsyncClient
The AsyncClient class is imported from the httpx library to make async HTTP calls.
Complete the code to create an async function that makes a GET request using AsyncClient.
async def fetch_data(url: str): async with AsyncClient() as client: response = await client.[1](url) return response.text
To retrieve data, the get method is used on the async client.
Fix the error in the async function to correctly return JSON data from the response.
async def fetch_json(url: str): async with AsyncClient() as client: response = await client.get(url) data = await response.[1]() return data
The json() method asynchronously parses the response body as JSON.
Fill both blanks to create a FastAPI route that calls the async fetch_data function and returns the result.
from fastapi import FastAPI app = FastAPI() @app.[1]("/data") async def get_data(): result = await fetch_data([2]) return {"data": result}
The route uses the @app.get decorator for GET requests and calls fetch_data with the URL string.
Fill all three blanks to create an async function that posts JSON data and returns the JSON response.
async def post_json(url: str, payload: dict): async with AsyncClient() as client: response = await client.[1](url, json=[2]) return await response.[3]()
The post method sends JSON data using the json parameter, and the response is parsed as JSON.