RequestsClient headers AttributeError

239 views
Skip to first unread message

Leo T

unread,
Jan 22, 2018, 4:29:16 PM1/22/18
to Django REST framework
The following code produces the following error. Have I missed something? I followed the attached doc.

Code:
client = RequestsClient()
client
.headers.update({'x-test': 'true'})

Error:
AttributeError: 'Client' object has no attribute 'headers'

Doc:


Thanks for the help

Leo T

unread,
Jan 22, 2018, 4:33:14 PM1/22/18
to Django REST framework
Sorry forgot to attach my config/dependencies:

Output of pip freeze:
certifi==2017.11.5
chardet==3.0.4
coverage==4.4.2
Django==2.0.1
django-reversion==2.0.12
django-reversion-compare==0.8.3
djangorestframework==3.7.7
djangorestframework-jwt==1.11.0
idna==2.6
PyJWT==1.5.3
pytz==2017.3
requests==2.18.4
urllib3==1.22

Xavier Ordoquy

unread,
Jan 22, 2018, 4:34:43 PM1/22/18
to django-res...@googlegroups.com
Hi,

Your code sample works fine on my installation.
Ensure you do use the DRF’s RequestsClient and not Django’s.

Regards,
Xavier,
Linovia.

--
You received this message because you are subscribed to the Google Groups "Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

signature.asc

Leo T

unread,
Jan 22, 2018, 4:47:09 PM1/22/18
to Django REST framework
Hi Xavier, I am using DRF's RequestsClient, here is the full code:

from django.test import TestCase
from dummy.models import Dummy
from rest_framework.test import RequestsClient
from requests import Session
from user.models import User

class DummyTestCase(TestCase):
client = RequestsClient()
url = '/dummies/'
headers = {'content-type': 'application/json'}
token = ''
headers = {}

def setUp(self):
Dummy.objects.create(name='this is a dummy test')
User.objects.create_user('username', 'email', 'Password1234')
result = self.client.post('/api-token-auth/', data={'username':'username','password':'Password1234'})
self.token = result.data['token']
self.headers['Authorization'] = 'JWT ' + self.token

def test_can_GET_dummies(self):
self.client.headers.update({'Authorization': 'JWT ' + self.token})
#print(self.headers)
response = self.client.get(self.url, headers=self.headers)
self.assertEqual(response.status_code, 200)
self.assertEqual(len(response.data), 1)


As you can see I am setting up a simple Django/DRF/JWT architecture and testing GET on a dummy model. Everything work fine using Postman, curl, etc.. But when I use this code and

python manage.py test

I get the above AttributeError on the headers of my RequestsClient object. If I don't include the faulty line, I get the expected HTTP 401 Unauthorized as I am not sending the JWT token (I removed Basic and Session authentication in the settings).

Any idea of what might cause the problem?

Thanks
Linovia.

To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-framework+unsub...@googlegroups.com.

Xavier Ordoquy

unread,
Jan 22, 2018, 4:53:38 PM1/22/18
to django-res...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
signature.asc

Leo T

unread,
Jan 22, 2018, 11:54:31 PM1/22/18
to Django REST framework
Thanks Xavier.

For anyone who might run into a similar problem with the same setup: I got it working by overriding the request client in the testcase's setup method.

Is there a reason to use unittest testcase instead of Django testcase? If not, would it make sense to add this caveat in the doc?

Xavier Ordoquy

unread,
Jan 23, 2018, 1:51:55 AM1/23/18
to django-res...@googlegroups.com
To unsubscribe from this group and stop receiving emails from it, send an email to django-rest-fram...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages