By default, APIClient sends data as form-encoded unless format='json' is specified.
Step 2: Understand why test fails
The API expects JSON data, so missing format='json' causes the server to reject or misinterpret data, failing the test.
Final Answer:
Missing format='json' in the post request -> Option A
Quick Check:
POST JSON data needs format='json' [OK]
Hint: Add format='json' when posting JSON data [OK]
Common Mistakes:
Assuming POST sends JSON by default
Confusing GET and POST methods
Ignoring import statements
5. You want to test an API endpoint that requires authentication. Which sequence correctly tests a protected GET endpoint using Django REST Framework's APIClient?
hard
A. Call client.get() first, then authenticate client with credentials
B. Set authentication headers manually without using client methods
C. Use client.post() without authentication to access the endpoint
D. Authenticate client with credentials, then call client.get() on the endpoint
Solution
Step 1: Authenticate the APIClient before requests
Use client.force_authenticate(user=user) or set credentials before making requests.
Step 2: Make the GET request after authentication
Once authenticated, call client.get() to access the protected endpoint successfully.
Final Answer:
Authenticate client with credentials, then call client.get() on the endpoint -> Option D
Quick Check:
Authenticate before GET request [OK]
Hint: Authenticate client before calling protected endpoint [OK]