Bird
0
0

You want to test an API endpoint that returns a list of items filtered by a query parameter category. Which code snippet correctly tests this filtering using APIClient?

hard📝 component behavior Q8 of 15
Django - Testing Django Applications
You want to test an API endpoint that returns a list of items filtered by a query parameter category. Which code snippet correctly tests this filtering using APIClient?
Aresponse = client.get('/api/items/?category=books')
Bresponse = client.post('/api/items/', data={'category': 'books'})
Cresponse = client.get('/api/items/', data={'category': 'books'})
Dresponse = client.get('/api/items/category/books')
Step-by-Step Solution
Solution:
  1. Step 1: Understand how to send query parameters in GET requests

    Query parameters are appended to the URL after a question mark.
  2. Step 2: Evaluate options for correct query parameter usage

    response = client.get('/api/items/?category=books') correctly appends '?category=books' to the URL; others misuse POST or data argument.
  3. Final Answer:

    response = client.get('/api/items/?category=books') -> Option A
  4. Quick Check:

    Query params go in URL for GET requests [OK]
Quick Trick: Use URL query string for GET filters [OK]
Common Mistakes:
MISTAKES
  • Using POST instead of GET for filtering
  • Passing query params in data argument for GET
  • Assuming URL path includes filter without API support

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes