Cameleer

Base64 data format

The base64 data 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

directionbody inbody out
marshalany body (text is taken as UTF-8 bytes)Base64 text, wrapped every lineLength characters
unmarshalBase64 text (standard or URL-safe alphabet; line breaks ignored)bytes

Example

Encode a body, then decode it again:

yaml example: base64-encode
- route:
    id: base64-encode
    from:
      uri: direct:base64-encode
      steps:
        - marshal:
            base64: {}
text
Hello, Cameleer!
text
SGVsbG8sIENhbWVsZWVyIQ==
yaml example: base64-decode
- route:
    id: base64-decode
    from:
      uri: direct:base64-decode
      steps:
        - unmarshal:
            base64: {}
        - convertBodyTo:
            type: String
text
SGVsbG8sIENhbWVsZWVyIQ==
text
Hello, Cameleer!

URL-safe output (no +, / or padding), handy for tokens in URLs:

yaml example: base64-url-safe
- route:
    id: base64-url-safe
    from:
      uri: direct:base64-url-safe
      steps:
        - marshal:
            base64:
              urlSafe: true
text
??>>
text
Pz8-Pg

Rust DSL

rust
b.from("direct:base64-rust")
    .marshal(DataFormatDefinition::base64().line_length(0));

Options

optiondefaultdescription
lineLength76Wrap the output after this many characters; 0 disables wrapping.
lineSeparatorCRLFSeparator inserted when wrapping (\n, \r\n).
urlSafefalseUse 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.

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.

Edit this page on GitHub