Skip to main content

drf-base64-binaryfield

PyPI Status Python Version License

Tests Codecov

pre-commit Black

Features

This package provides a Base64BinaryField for use with Django REST Framework serializers.

It allows you to:

  • Serialize binary data into base64 strings for use in JSON responses
  • Deserialize base64 strings back into binary data
  • Optionally use url-safe base64 encoding (replacing + and / with - and _)
  • Validate the length of the binary data

Why? because JSON does not support binary data, and base64 is a common way to represent binary data in JSON.

Considerations

Base64 encoding isn't very space efficient.

If you need to send a lot of binary data quite often, you may want to consider using a more efficient serialization format instead of JSON, such as MessagePack. There is a MessagePack serializer for Django REST Framework available: django-rest-framework-msgpack.

If you only occasionally need to send binary data then base64 encoding is probably fine for your use case. This package may be suitable for you.

Requirements

  • Python 3.9+
  • Django 4.2+
  • Django REST Framework 3.14+

Installation

You can install drf-base64-binaryfield via pip from PyPI:

$ pip install drf-base64-binaryfield

Usage

In this example, we need to send a cryptographic challenge to the client that consists of raw bytes. Wouldn't it be convenient if there was a way to send this data as part of JSON response?

We can use Base64BinaryField provided by this package to serialize the binary data into a base64 string, which can be sent as part of a JSON response.

from rest_framework import serializers
from drf_base64_binaryfield.fields import Base64BinaryField

class ChallengerSerializer(serializers.Serializer):
    # This field accepts a Python bytes object and serializes it into a base64 string. Or it can deserialize a base64 string back into a bytes object.
    challenge = Base64BinaryField()

serializer = ChallengerSerializer(instance={'challenge': b'\x00\x01\x02\x03'})
print(serializer.data)
# {'challenge': 'AAECAw=='}

Web-safe encoding

If you want to use web-safe base64 encoding, you can set url_safe=True:

class CryptographicChallengeSerializer(serializers.Serializer):
    challenge = Base64BinaryField(url_safe=True)

Binary data size validation

This package also supports validating the size of the decoded binary data:

class ExampleSerializer(serializers.Serializer):
    # This field will only accept binary data that is between 16 and 32 bytes long
    example_binary = Base64BinaryField(min_size=16, max_size=32)

Extending Base64BinaryField

You can extend Base64BinaryField to create your own custom fields. You may want to unpack the binary data into a different format, for example.

import struct

class CustomBinaryField(Base64BinaryField):
    def to_internal_value(self, data):
        binary_data = super().to_internal_value(data)

        # Do something with the binary data...

        # For example: unpack it as a little-endian, 32-bit unsigned integer
        return struct.unpack('<I', binary_data)[0]

Contributing

Contributions are very welcome. To learn more, see the Contributor Guide.

License

Distributed under the terms of the MIT license, drf-base64-binaryfield is free and open source software.

Issues

If you encounter any problems, please file an issue along with a detailed description.

Credits

This project was generated from @OmenApps's Cookiecutter Django Package template.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

drf_base64_binaryfield-1.2.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

drf_base64_binaryfield-1.2.0-py3-none-any.whl (8.6 kB view details)

Uploaded Python 3

File details

Details for the file drf_base64_binaryfield-1.2.0.tar.gz.

File metadata

  • Download URL: drf_base64_binaryfield-1.2.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.13.2 Darwin/24.3.0

File hashes

Hashes for drf_base64_binaryfield-1.2.0.tar.gz
Algorithm Hash digest
SHA256 b5c926a8c53e5ea1ec584f399641bf03b9919530757db13f7dfb7e038b9d82f4
MD5 6e22a6c1fc493e51ebf8d8144e488666
BLAKE2b-256 eb589ff20d87d2e17f246d1cdd75b0b4ac049d1abae913ea7c7830ba0313e97d

See more details on using hashes here.

File details

Details for the file drf_base64_binaryfield-1.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for drf_base64_binaryfield-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 3a90f665880d82a9b5644e0c141a507203e653971250c29a51ee69ffcc9870ec
MD5 a318c78ba3078f78590da01329cf235b
BLAKE2b-256 4f39bf9d0facbd864fe390a2853d027447bb289fea04e10739e795bbba2e6baa

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 files

1.1.0

2 files

1.0.0

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page