# How to encode/decode string using django.utils.http

**URL:** <https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504>\
**Category:** Forms & APIs\
**Created:** [June 27, 2022, 3:58pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504 "2022-06-27T15:58:57Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [June 27, 2022, 3:58pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/1 "2022-06-27T15:58:57Z")

</div>

Hi there,

I am trying to pass a user ID (primary key) to url as base 64 encoded. I am using `django.utils.http.urlsafe_base64_encode` to encode the user ID as the following:

```auto
from django.utils.http import urlsafe_base64_encode

user_id_base_64 = urlsafe_base64_encode(str(user.pk).encode('utf-8'))

```

This works fine and I get the desired 64 base byte of user id. The url pattern to hold this user id 64 base is:

```auto
path("password-reset/<uidb64>/, views.dashboard())

```

But I need to decode the same in the view which is accessed by the above url.

I try to do that with the following:

```auto
from django.utils.http import urlsafe_base64_decode

.
.
.
user_id = urlsafe_base64_decode(uidb64)

```

This does not give me the correct result, i.e. a numeric string or an integer.

Can someone help me how do I decode it properly to get the correct value?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2022, 4:08pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/2 "2022-06-27T16:08:31Z")

</div>

> [@khaw235](#):
>
> `path("password-reset/<uidb64>/, views.dashboard())`

Most obvious to me is the mistake here. Unless you’re doing something really unusual here, the definition would be:  
`path("password-reset/<uidb64>/", views.dashboard)`

Beyond that, it would be helpful if you showed an example of one of the keys, what that translates to for the url, and what you’re seeing in the view.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [June 27, 2022, 4:27pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/3 "2022-06-27T16:27:26Z")

</div>

Actually it was just a typo here in this post. In reality I have the same definition of path as you told in my code.

Well, the I have the following output when I print `user.key`:

```auto
>>> from .models import User
>>> user = User.objects.get(pk=1)
>>> print(user.pk)
1

```

and when I get the following output of encoding:

```auto
>>> from django.utils.http import urlsafe_base64_encode
>>> user_id_base_64 = urlsafe_base64_encode(str(user.pk).encode('utf-8'))
>>> print(user_id_base_64)
MQ

```

Now when I try to decode, I get the following output:

```auto
>>> from django.utils.http import urlsafe_base64_decode
>>> user_key = urlsafe_base64_decode(user_id_base_64)
>>> print(user_key)
b'1'

```

So, you can see instead of getting back `1` either in form of `str` or `int`, I am getting `b'1'`.

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2022, 4:31pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/4 "2022-06-27T16:31:20Z")

</div>

It’s decoding as a bytes object; you can then [decode it as a string](https://docs.python.org/3/library/stdtypes.html?highlight=bytes#bytes.decode).

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [June 27, 2022, 4:35pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/5 "2022-06-27T16:35:11Z")

</div>

Doing the following:

```auto
user_key = urlsafe_base64_decode(user_id_base_64.decode('str'))

```

gives me the following error:

```auto
Traceback (most recent call last):
  File "<string>", line 6, in <module>
AttributeError: 'bytes' object has no attribute 'encode'

```

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 27, 2022, 4:44pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/6 "2022-06-27T16:44:23Z")

</div>

It’s the output of `urlsafe_base64_decode` that is returning the bytes object, what you are printing as `user_key` - _that_ is what you want to decode as a string.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [June 27, 2022, 4:56pm UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/7 "2022-06-27T16:56:10Z")

</div>

Upon doing the following

```auto
user_key = urlsafe_base64_decode(user_key_base_64).decode('str')

```

I get the following error:

```auto
Traceback (most recent call last):
  File "<string>", line 6, in <module>
LookupError: unknown encoding: str

```

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [June 28, 2022, 9:38am UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/8 "2022-06-28T09:38:52Z")

</div>

@KenWhitesell any solution to this problem?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 28, 2022, 10:01am UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/9 "2022-06-28T10:01:36Z")

</div>

What do you think that error message is telling you?

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [June 28, 2022, 10:25am UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/10 "2022-06-28T10:25:31Z")

</div>

Its saying that it does not recognize `str` encoding. So what should I do to make it recognize or what’s the alternative method?

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [June 28, 2022, 10:34am UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/11 "2022-06-28T10:34:29Z")

</div>

Did you read the documentation I referenced? It shows what all the valid encodings are.

---

<div class="post-metadata">

**Author:** ![khaw235](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/khaw235/32/6372_2.png) [@khaw235](https://forum.djangoproject.com/u/khaw235)\
**Post date:** [June 28, 2022, 10:38am UTC](https://forum.djangoproject.com/t/how-to-encode-decode-string-using-django-utils-http/14504/12 "2022-06-28T10:38:01Z")

</div>

OK, I got it, I changed `'str'` to `'ascii'` and I got the original string back, now just needed to convert it to integer using `int()` method. Thanks a lot.
