The Simple Message Queue (formerly MNS) client provides built-in options for Base64 encoding. This topic describes the scenarios in which you should use Base64 encoding for message production and consumption.
Background
Base64 converts binary data into ASCII strings for safe transfer through text-based environments.
Advantages of Base64 encoding
-
Compatibility: Base64-encoded strings use only printable ASCII characters, making them safe for text-based formats such as email, JSON, and XML without risking garbled output.
-
Security: Base64 is not encryption, but it prevents binary data from being directly readable in some plain text formats. This adds a layer of security.
-
Simplicity: Most programming languages and libraries provide built-in Base64 support.
Disadvantages of Base64 encoding
-
Data bloat: Base64 encoding increases the data size by approximately 33%.
-
Reduced efficiency: Encoding and decoding require additional computing resources. The performance overhead can be significant, especially when processing large amounts of data.
-
Low readability: Encoded message bodies are not human-readable.
When to use Base64 encoding
Queue-based messaging model
Skip Base64 encoding if the message body contains no special characters.
-
To send a message, call the
message.setMessageBodyAsRawStringmethod. -
To receive a message, call the
message.getMessageBodyAsRawStringmethod.
Topic-based model (queue/HTTP subscription)
In the topic-based model, a subscription's NotifyContentFormat property determines the push message format: SIMPLIFIED, JSON, or XML, as described in Subscription formats.
-
SIMPLIFIED format: Skip Base64 encoding if the message body contains no special characters.
-
To send a message, initialize the message object with
RawTopicMessage. -
To consume a message, call
message.getMessageBodyAsRawString().
-
-
JSON or XML format: You must use Base64 encoding because Base64-encoded strings are suitable for transfer in text formats such as JSON and XML.
-
To send a message, initialize the message object with TopicMessage. The body is automatically Base64-encoded and placed in the Message field.
-
To consume a message, call
message.getMessageBodyAsRawString();to get the Message field value, then Base64-decode it.JSONObject object = new JSONObject(message.getMessageBodyAsRawString()); String jsonMessageData = String.valueOf(object.get("Message")); String messageBody = new String(Base64.decodeBase64(jsonMessageData));
-
Topic-based model (email subscription)
Skip Base64 encoding if the message body contains no special characters.
-
To send a message, call the
message.setMessageBodyAsRawStringmethod. -
To receive a message, call the
message.getMessageBodyAsRawStringmethod.
Topic-based model (text message subscription)
Skip Base64 encoding if the message body contains no special characters.
-
To send a message, call the
message.setMessageBodyAsRawStringmethod. -
To receive a message, call the
message.getMessageBodyAsRawStringmethod.
Topic-based model (Mobile Push subscription)
Skip Base64 encoding if the message body contains no special characters.
-
To send a message, call the
message.setMessageBodyAsRawStringmethod. -
To receive a message, call the
message.getMessageBodyAsRawStringmethod.