Thanks to visit codestin.com
Credit goes to github.com

Skip to content

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

Closed
4 of 6 tasks
skolsuper opened this issue Aug 25, 2016 · 6 comments
Closed
4 of 6 tasks

APIRequestFactory returns WSGIRequest objects #4440

skolsuper opened this issue Aug 25, 2016 · 6 comments

Comments

@skolsuper
Copy link

skolsuper commented Aug 25, 2016

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

  • I have verified that that issue exists against the master branch of Django REST framework.
  • I have searched for similar issues in both open and closed tickets and cannot find a duplicate.
  • This is not a usage question. (Those should be directed to the discussion group instead.)
  • This cannot be dealt with as a third party library. (We prefer new functionality to be in the form of third party libraries where possible.)
  • I have reduced the issue to the simplest possible case.
  • I have included a failing test as a pull request. (If you are unable to do so we can still accept the issue.)

Steps to reproduce

    api_rf = APIRequestFactory()
    request = api_rf.post('/some/url', {})
    request.data.get('foo')

Expected behavior

None

Actual behavior

AttributeError: 'WSGIRequest' object has no attribute 'data'

@decentral1se
Copy link
Contributor

This is a duplicate of #3608 (closed).

You can probably achieve what you want by following #3608 (comment).

@skolsuper
Copy link
Author

skolsuper commented Sep 1, 2016

So it is. Thanks for the heads-up 👍 .

@tomchristie
Copy link
Member

Aside: I have warmed to the idea of ensuring that this returns a fully fledged Request instance, and having views be able to handle either, so this might come up in the future.

@technodivesh
Copy link

try
request.DATA
(caps)

@rpkilby
Copy link
Member

rpkilby commented Feb 28, 2019

@property
def DATA(self):
raise NotImplementedError(
'`request.DATA` has been deprecated in favor of `request.data` '
'since version 3.0, and has been fully removed as of version 3.2.'
)

@mesajidiqbal
Copy link

To resolve this issue, you need to wrap the APIRequestFactory request with rest_framework.request.Request. This will provide the necessary context and attributes expected in DRF views, including the data attribute.

Here's an example of how to correctly use APIRequestFactory for both GET and POST requests:

# 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()])

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants