14

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.

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)
3
  • Send the login details to your login url with a post request via Postman. I dont know how your authentication works, but your API should return some sort of access token, which you can probably use to access the rest of the API Commented Jun 6, 2018 at 8:16
  • Continuing on from @Snackoverflow, then you should be able to use the access token using something like stackoverflow.com/questions/40539609/… Commented Jun 6, 2018 at 8:17
  • Authentication is not the token base. It is normal Django authentication using username and password. May we can send cookies info in a way the browser sends to the server. Commented Jun 6, 2018 at 8:20

2 Answers 2

15

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.

Sign up to request clarification or add additional context in comments.

6 Comments

I have tried this one but it's not working. Normally Django varifies authenticated a user through session and cookies.
Can you add your respective view.py?
Peter I have added view in question.
seems like you only added permission_classes in views. Did you add any other authentication methods in settings?
No, I am not using any other authentication model in settings. Even I remove permission classes except for IsAuthenticated. I still face the same issue.
|
4

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',
...

Comments

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.