Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test Django REST Framework Login Protected API using postman?

I need to test REST API using postman. API is build using Django REST Framework. Only login user can get access to API. I am not able to find how I can send login credentials using postman. Thanks in advance.

class ApiMemberGroupNameList(views.APIView):
    permission_classes = (
        permissions.IsAuthenticated,
        RequiredOrgPermission,
        RequiredOrgStaffMemberPermission)

    def get(self, request, **kwargs):
        pk = kwargs.get('pk')
        hash = kwargs.get('hash')
        member_obj = get_object_or_404(Member.objects.filter(org__hash=hash, pk=pk))
        return Response(GroupListSerializer(member_obj.groups.all(), many=True).data)
like image 533
Shahzad Farukh Avatar asked Jun 06 '18 08:06

Shahzad Farukh


People also ask

What is Knox in Django REST Framework?

Django-Knox is a framework that makes the authentication of the API endpoints built with the Django Rest Framework easier. However, Knox is also a token-based authentication like JSON Web Token (JWT) auth. Django-Knox comes with well-detailed documentation for easy implementation.

How do I run test cases in Django REST Framework?

Once you have coded a couple of tests, you can execute them with this command in the main folder of the project (the one that has the manage.py file): python manage.py test This will search for all of your tests files under that folder and then execute them, showing the error or success of each unit test.


2 Answers

You can use Basic Auth in POSTMAN. Refer to this screenshot:

enter image description here

You could change the HTTP Methods, URLs, Data Payload(under Body tab) etc in the POSTMAN console

UPDATE-1

After your comments, I tried to recreated the problem.What I did was:

  1. created a view

     from rest_framework.views import APIView
     from rest_framework.permissions import IsAuthenticated
     from rest_framework.response import Response
    
     class MySampleView(APIView):
         permission_classes = (IsAuthenticated,)
    
         def get(self, request):
             return Response(data={"status": True})
    
  2. Added to urls.py

     urlpatterns = [
         url(r'^mysampleview/$', MySampleView.as_view())
     ]
    

And my POSTMAN response are below:

Authorization Screenshot

Header Screenshot

My conclusion

You may enter wrong credentials, or something else. I would suggest you open a new POSTMAN tab and repeat the procedure, also try to login Django admin using the same credential which is already used in POSTMAN.

like image 106
JPG Avatar answered Nov 02 '22 07:11

JPG


If you want to use Basic Authentification to test Django REST API, it must be allowed in Django REST Framework settings:

'DEFAULT_AUTHENTICATION_CLASSES': [
        'rest_framework.authentication.BasicAuthentication',
...
like image 28
Viktoriya Dorosheva Avatar answered Nov 02 '22 07:11

Viktoriya Dorosheva