-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
APIRequestFactory returns WSGIRequest objects #4440
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This is a duplicate of #3608 (closed). You can probably achieve what you want by following #3608 (comment). |
So it is. Thanks for the heads-up 👍 . |
Aside: I have warmed to the idea of ensuring that this returns a fully fledged |
try |
django-rest-framework/rest_framework/request.py Lines 416 to 421 in 1dc81ac
|
To resolve this issue, you need to wrap the Here's an example of how to correctly use # Imports
from rest_framework.test import APIRequestFactory
from rest_framework.request import Request
from rest_framework.parsers import JSONParser
import json
# Initialize the APIRequestFactory
request_factory = APIRequestFactory()
# Query Param or Data
data = {"key": "value"}
# Example for a GET request
request = request_factory.get('/api/url/', data)
request = Request(request)
# Example for a POST request
json_data = json.dumps(data) # data must be converted to string for POST requests
request = request_factory.post('/api/url/', data=json_data, content_type='application/json')
request = Request(request, parsers=[JSONParser()]) |
I am trying to test a custom permissions class using APIRequestFactory to fabricate a request. The custom permissions object checks the request body for an OTP using
request.data
, a convenience property added by DRF Request objects. However this fails because APIRequestFactory directly returns the WSGIRequest object returned by Django's RequestFactory.Checklist
master
branch of Django REST framework.Steps to reproduce
Expected behavior
None
Actual behavior
AttributeError: 'WSGIRequest' object has no attribute 'data'
The text was updated successfully, but these errors were encountered: