Prometheus Prometheus - 3 months ago 21
Python Question

Testing in Django Rest Framework, reproduce this curl request?

I am trying to run some testing for Django Rest Framework (links to testing) and reproduce this curl call:

curl -X POST -d "grant_type=password&username=<user_name>&password=<password>"
http://<client_id>:<client_secret>@127.0.0.1:8000/v1/token/


This is what I have tried:

def test_oauth2_token_for_user(self):
"""
Test Django OAuth Toolkit and make a request for am access token.
"""
self.client.credentials(HTTP_AUTHORIZATION='grant_type ' + "password&username=<user_name>&password=<password")
response = self.client.post('/api/token/')


Could someone tell me where to start. I am unsure how to send the header
grant_type=password&username=<user_name>&password=<password>
and format the URL with the username and password.

Answer

That's not a header. That's perfectly normal POST data, which - as the test client documentation shows - you can just pass in a dictionary as the second parameter to post.

response = self.client.post('/api/token/',
       {'grant_type': 'password', 'username': username, 'password': password})