1

I am trying to decode Django's session data stored in redis DB to JSON object. I have got session data from redis DB into a variable djangoSessionData, when in do console.log of this data, it looks like this:

���}    �(�_auth_user_backend��)django.contrib.auth.backends.ModelBackend��_auth_us    er_hash_auth_user_id��2��user_unique_key��abc�u.65��

When I query redis directly and see session data value, it is like this:

"\x80\x04\x95\xaf\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x12_auth_user_backend\x94\x8c)django.contrib.auth.backends.ModelBackend\x94\x8c\x0f_auth_user_hash\x94\x8c(6d34e7154c4d217233c7346177325969d1832565\x94\x8c\r_auth_user_id\x94\x8c\x012\x94\x8c\x0fuser_unique_key\x94\x8c\x03abc\x94u."

I am trying to decode it into JSON string using:

var sessionData = new Buffer(djangoSessionData, 'base64').toString();

But when I console.log sessionData, it looks like this:

������z��i���cjx(r���&ں�[i���
                         �ץ�$zwj�a����Z��߇�מ�ݵ�m�s���^��n}��u�}���ں��ǫ�'v�ǫ����翑��m˿

So looks like it is not decoding properly, how can it be decoded to JSON object in Node.js

Edit: I am using Django 1.9 and saving session to redis DB using:

SESSION_ENGINE = 'django.contrib.sessions.backends.cached_db'
Harshveer Singh
  • 4,037
  • 7
  • 37
  • 45
  • Django sessions, I believe, are pickled by default. You can change that encoding to JSON instead. http://stackoverflow.com/questions/24229397/django-object-is-not-json-serializable-error-after-upgrading-django-to-1-6-5 – tsturzl Dec 25 '15 at 19:43
  • @tsturzl with Django 1.6+ default session serializer is JSONSerializer. I also explicitly set it to JSONSerializer(`SESSION_SERIALIZER = 'django.contrib.sessions.serializers.JSONSerializer'`) but it doesn't make difference. – Harshveer Singh Dec 25 '15 at 19:56
  • Seems that cache adaptor is still pickling the data. Someone forked the cache adaptor to add a JSON serializer, https://github.com/hellysmile/django-redis-sessions-fork – tsturzl Jan 08 '16 at 04:56

0 Answers0