APIClient and accept_language header

35 views
Skip to first unread message

Daviddd

unread,
Apr 14, 2018, 1:41:49 AM4/14/18
to Django REST framework
Dear all,

Using APIClient (not the requests based class) is it possible to pass as param the accept_language?


class BaseAPITestCase(APITestCase):
   
@classmethod
   
def setUpTestData(cls):
       
def _get_jwt_token(user_credentials):
            url_token_auth
= reverse('api:default:api-token-auth')
            response
= cls.client.post(url_token_auth, user_credentials, format='json')
           
assert response.status_code == status.HTTP_200_OK
           
return response.data['token']

        cls
.client = APIClient(enforce_csrf_checks=True)

        cls
.user1 = {}
        cls
.user1['user'] = get_user_model().objects.get(username='user1')
        cls
.user1['auth'] = 'JWT {0}'.format(_get_jwt_token({'username': cls.user1['user'].username,
                                                             
'password': 'password'}))

class TestBase(BaseAPITestCase):
   
def test_flags(self):
        url
= reverse('api:default:flags-list')
        response
= self.client.get(url, HTTP_AUTHORIZATION=self.user1['auth'])
       
self.assertEqual(response.status_code, status.HTTP_200_OK)



Using the JSON Token it works great 
response = self.client.get(url, HTTP_AUTHORIZATION='JWT <value>')
but this doesn't
response = self.client.get(url, ACCEPT_LANGUAGE='es', HTTP_AUTHORIZATION='JWT <value>')


I would need to mimic: 

curl -H "Accept-Language: it" http://localhost:8001/v1/flags/
curl -H "Accept-Language: en" http://localhost:8001/v1/flags/
curl -H "Accept-Language: es" http://localhost:8001/v1/flags/

Thanks.

D

Carlton Gibson

unread,
Apr 16, 2018, 8:15:47 AM4/16/18
to django-res...@googlegroups.com
Hi David, 

On 14 Apr 2018, at 07:41, Daviddd <davide...@gmail.com> wrote:

ACCEPT_LANGUAGE='es'

I think you’d need to pass it like this:

    HTTP_ACCEPT_LANGUAGE=‘es'


Kind Regards,

Carlton

Daviddd

unread,
Apr 18, 2018, 12:49:54 AM4/18/18
to Django REST framework
Thanks Carlton, yeap it works. I also used HTTP_ACCEPT_LANGUAGE=‘es', but after your reply I found the error in my code. It was very difficult to spot it without your answer.
Reply all
Reply to author
Forward
0 new messages