Base64 data format
The
base64data format encodes the message body as Base64 text (marshal) and decodes Base64 text back to bytes (unmarshal).
Name: base64 · Crate: cameleer-core · Cargo feature: none, always included (build features) ·
Apache Camel: Base64
When to use it
- Put binary data (an image, a PDF, a compressed payload) into a JSON field, an email body or an HTTP header that only carries text.
- Send an image to a multimodal model API that expects Base64 content.
- Decode Base64 attachments or tokens received from another system.
Marshal and unmarshal
| direction | body in | body out |
|---|---|---|
marshal | any body (text is taken as UTF-8 bytes) | Base64 text, wrapped every lineLength characters |
unmarshal | Base64 text (standard or URL-safe alphabet; line breaks ignored) | bytes |
Example
Encode a body, then decode it again:
- route:
id: base64-encode
from:
uri: direct:base64-encode
steps:
- marshal:
base64: {}Hello, Cameleer!SGVsbG8sIENhbWVsZWVyIQ==- route:
id: base64-decode
from:
uri: direct:base64-decode
steps:
- unmarshal:
base64: {}
- convertBodyTo:
type: StringSGVsbG8sIENhbWVsZWVyIQ==Hello, Cameleer!URL-safe output (no +, / or padding), handy for tokens in URLs:
- route:
id: base64-url-safe
from:
uri: direct:base64-url-safe
steps:
- marshal:
base64:
urlSafe: true??>>Pz8-PgRust DSL
b.from("direct:base64-rust")
.marshal(DataFormatDefinition::base64().line_length(0));Options
| option | default | description |
|---|---|---|
lineLength | 76 | Wrap the output after this many characters; 0 disables wrapping. |
lineSeparator | CRLF | Separator inserted when wrapping (\n, \r\n). |
urlSafe | false | Use the URL-safe alphabet (- and _) without padding. |
Common questions
How do I Base64-encode a file for an API request?
Read the file, marshal: {base64: {lineLength: 0}}, then put ${body} into the JSON request. Use
lineLength: 0 so the text has no line breaks.
How do I encode a single header instead of the body?
Use the Simple functions ${base64Encode(${header.x})} and ${base64Decode(...)}; see the
Simple language.
Why does the decoded body look like bytes?
unmarshal returns bytes. Add convertBodyTo: {type: String} when the content is text.
Related
- Hex data format, String data format, GZip data format
- Simple language
base64Encode/base64Decodefunctions - Apache Camel Base64 data format
Found a problem?
If something on this page doesn't match what Cameleer does, or you hit a bug while building
with it, please help fix it, whether you are a person or a coding agent:
open an issue that describes what you saw,
then fork the repository, fix it, add or update a test, run cargo test --workspace, and open
a pull request. CONTRIBUTING.md
explains the ground rules.