Bird
0
0

Inside a Django async view, you want to fetch JSON data from an external API using the httpx library asynchronously. Which snippet correctly performs this operation?

hard📝 component behavior Q8 of 15
Django - Async Django
Inside a Django async view, you want to fetch JSON data from an external API using the httpx library asynchronously. Which snippet correctly performs this operation?
Aresponse = httpx.get('https://api.example.com/data') data = response.json()
Bclient = httpx.Client() response = client.get('https://api.example.com/data') data = response.json()
Casync with httpx.AsyncClient() as client: response = await client.get('https://api.example.com/data') data = response.json()
Dasync def fetch(): response = httpx.get('https://api.example.com/data') return response.json()
Step-by-Step Solution
Solution:
  1. Step 1: Use async client

    To perform async HTTP requests with httpx, use httpx.AsyncClient() inside an async context.
  2. Step 2: Await the request

    Await the client.get() coroutine to avoid blocking the event loop.
  3. Step 3: Parse JSON after awaiting

    Once awaited, call response.json() synchronously to get the data.
  4. Final Answer:

    async with httpx.AsyncClient() as client: response = await client.get('https://api.example.com/data') data = response.json() -> Option C
  5. Quick Check:

    Use AsyncClient and await HTTP calls in async views [OK]
Quick Trick: Always await async client requests in async views [OK]
Common Mistakes:
MISTAKES
  • Using synchronous httpx.Client in async views
  • Not awaiting async HTTP calls
  • Calling httpx.get() without await in async context

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes