return jwt token as json

104 views
Skip to first unread message

Rakhee Menon

unread,
Oct 29, 2017, 1:32:56 PM10/29/17
to Django users
Unable to send jwt token as json response

Jani Tiainen

unread,
Oct 29, 2017, 4:35:34 PM10/29/17
to django...@googlegroups.com
As a simplest possible form you can pass jwt as s string:

"Xxx.yyyhgg.cxxch"

29.10.2017 19.33 "Rakhee Menon" <menonr...@gmail.com> kirjoitti:
Unable to send jwt token as json response

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/742f8637-b9e2-4d13-9ea7-0764fb45f5e9%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Rakhee Menon

unread,
Oct 31, 2017, 7:37:25 AM10/31/17
to Django users


class Login(APIView):
  
   def post(self, request, *args, **kwargs):
       import ipdb;ipdb.set_trace()
       username = request.POST.get('username')
       password = request.POST.get('password')
       user = Person.objects.get(username=username, password=password)
#        user = user[0]
       #import ipdb;ipdb.set_trace()
       if user:
           payload = {
               'id': user.pk,
               'username': user.username,
               'staff': user.email,
               'exp': datetime.utcnow()
           }
           token = {'token': jwt.encode(payload, self.SECRET)}
           return HttpResponse(
             json.dumps(token),
             content_type="application/json"
           )
       else:
           return HttpResponse(
             json.dumps({'Error': "Invalid credentials"}),
             status=400,
             content_type="application/json"
           )





I have written a login api which which should return the token if user is valid and then would save the same on client side.
But my api is getting this following error..


TypeError at /login/
 is not JSON serializable

James Schneider

unread,
Nov 3, 2017, 3:37:39 AM11/3/17
to django...@googlegroups.com


On Oct 31, 2017 4:36 AM, "Rakhee Menon" <rakhee....@gmail.com> wrote:


class Login(APIView):
  
   def post(self, request, *args, **kwargs):
       import ipdb;ipdb.set_trace()
       username = request.POST.get('username')
       password = request.POST.get('password')
       user = Person.objects.get(username=username, password=password)
#        user = user[0]
       #import ipdb;ipdb.set_trace()
       if user:
           payload = {
               'id': user.pk,
               'username': user.username,
               'staff': user.email,
               'exp': datetime.utcnow()

I think that datetime.utcnow() returns a datetime object, not a string, and I don't think it can be serialized directly. You should try converting that object to a string in the right format. As a test, substitute a static string containing a date and time in whatever format you need. I'm not in front of an interpreter so I can't test, but this seems to back up my theory:


-James

James Schneider

unread,
Nov 3, 2017, 3:42:12 AM11/3/17
to django...@googlegroups.com

       if user:
           payload = {
               'id': user.pk,
               'username': user.username,
               'staff': user.email,
               'exp': datetime.utcnow()

I think that datetime.utcnow() returns a datetime object, not a string, and I don't think it can be serialized directly. You should try converting that object to a string in the right format. As a test, substitute a static string containing a date and time in whatever format you need. I'm not in front of an interpreter so I can't test, but this seems to back up my theory:


Forgot to mention, you may also want to take a look at the built-in JSON encoder, which handles datetime objects:


-James

Jani Tiainen

unread,
Nov 3, 2017, 4:54:06 AM11/3/17
to django...@googlegroups.com
According to pyjwt docs it can be either datetime object or number (unix epoch)



--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
Reply all
Reply to author
Forward
0 new messages