0

I am trying to do simple tests on my newly created viewsets, by using the APIClient class instead of APIRequestFactory, however, the view fails with the following error:

AttributeError: 'Response' object has no attribute 'encoding'

It works fine if I use APIRequestFactory though, but I need APIClient since I am using TokenAuthentication.

This is the context in which I am using the client:

api_client = APIClient()
reversed_url = reverse('showcase:ajax:publisher-dashboard', kwargs={'pk': 1})
client_token = cm.Client.objects.get(first_name='Cliente 1',
                                     last_name='Prueba Showcase').user.auth_token.key
api_client.credentials(HTTP_AUTHORIZATION='Token ' + client_token)

request = api_client.get(reversed_url, format='json')
response = views.PubViewSet.as_view({'get': 'dashboard'})(request, pk=1)

The results are the same without providing credentials and disabling TokenAuth in settings.py.

These are the contents of the Response object when using APIRequestFactory:

<Response status_code=200, "text/html; charset=utf-8">

_charset = {NoneType} None
_closable_objects = {list} <class 'list'>: []
_container = {list} <class 'list'>: [b'']
_content_type_for_repr = {str} ', "text/html; charset=utf-8"'
_handler_class = {NoneType} None
_headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')}
_is_rendered = {bool} False
_post_render_callbacks = {list} <class 'list'>: []
_reason_phrase = {NoneType} None
_request = {NoneType} None
charset = {str} 'utf-8'
closed = {bool} False
content = {str} 'Traceback (most recent call last):\n  File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n    attr = getattr(var, n)\n  File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
content_type = {NoneType} None
context_data = {NoneType} None
cookies = {SimpleCookie} 
data = {dict} {'publisher': {'id': 1, 'first_name': '', 'last_name': '', 'name': 'TEST', 'official_name': 'TEST, S.A.', 'rfc': 'XAXXXXXXX000X', 'street': 'St.', 'number': '420', 'interior': '', 'neighborhood': '', 'zipcode': '', 'email': '', 'phone': '', 'image': '/rcro
exception = {bool} False
is_rendered = {bool} False
reason_phrase = {str} 'OK'
rendered_content = {str} 'Traceback (most recent call last):\n  File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n    attr = getattr(var, n)\n  File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
rendering_attrs = {list} <class 'list'>: ['template_name', 'context_data', '_post_render_callbacks']
status_code = {int} 200
status_text = {str} 'OK'
streaming = {bool} False
template_name = {NoneType} None
using = {NoneType} None

And when APIClient is used:

<Response status_code=200, "text/html; charset=utf-8">

_charset = {NoneType} None
_closable_objects = {list} <class 'list'>: []
_container = {list} <class 'list'>: [b'']
_content_type_for_repr = {str} ', "text/html; charset=utf-8"'
_handler_class = {NoneType} None
_headers = {dict} {'content-type': ('Content-Type', 'text/html; charset=utf-8')}
_is_rendered = {bool} False
_post_render_callbacks = {list} <class 'list'>: []
_reason_phrase = {NoneType} None
_request = {NoneType} None
charset = {str} 'utf-8'
closed = {bool} False
content = {str} 'Traceback (most recent call last):\n  File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n    attr = getattr(var, n)\n  File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
content_type = {NoneType} None
context_data = {NoneType} None
cookies = {SimpleCookie} 
data = {dict} {'publisher': {'id': 1, 'first_name': '', 'last_name': '', 'name': 'TEST', 'official_name': 'TEST, S.A.', 'rfc': 'XAXXXXXXX000X', 'street': 'St.', 'number': '420', 'interior': '', 'neighborhood': '', 'zipcode': '', 'email': '', 'phone': '', 'image': '/rcro
exception = {bool} False
is_rendered = {bool} False
reason_phrase = {str} 'OK'
rendered_content = {str} 'Traceback (most recent call last):\n  File "/home/ariel/.local/pycharm-2017.1.1/helpers/pydev/_pydevd_bundle/pydevd_resolver.py", line 197, in _getPyDictionary\n    attr = getattr(var, n)\n  File "/home/ariel/.virtualenvs/recrow-app/lib/python3.6/site-packag
rendering_attrs = {list} <class 'list'>: ['template_name', 'context_data', '_post_render_callbacks']
status_code = {int} 200
status_text = {str} 'OK'
streaming = {bool} False
template_name = {NoneType} None
using = {NoneType} None

1 Answer 1

1

I think you have error in this lines of code:

request = api_client.get(reversed_url, format='json')
response = views.PubViewSet.as_view({'get': 'dashboard'})(request, pk=1)

api_client.get already return HTTPResponse, so you dont need to pass it to VewSet as request. Instead of it try this:

response = api_client.get(reversed_url)
assert response.status_code == 200
Sign up to request clarification or add additional context in comments.

1 Comment

You are completely right, I thought it was a drop in replacement for APIRequestFactory but it actually generates a complete get request to the specified URL. I had to take a second look to Django's original Client class to verify this. Thank you for clarifying this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.