---
schema_version: "1"
id: 395a657602f562fb768449820353c43171b1eaa6a984e2e058cd05bd21a46dc5
title: Decode and Encode Base64
collection: mulesoft
version: current
locale: en-us
portal_url: https://docs.salesforce.com/mulesoft/current/en-us/dataweave/2.6/dataweave-cookbook-decode-encode.md
canonical_url: https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook-decode-encode
source_site: docs.mulesoft.com
content_type: documentation
content_hash: 872b81972ca974ebcd7d2f2ccf3383bfb64fe152ee01826ca037cd23b1ba5203
description: MuleSoft Documentation Site
release: "2.6"
latest: false
---
---
product: DataWeave
version: 2.6
keywords: studio, anypoint, transform, transformer, format, xml, metadata, dataweave, data weave, datamapper, dwl, dfl, dw, output structure, input structure, map, mapping
---

# Decode and Encode Base64

> For the full documentation index, see: https://docs.mulesoft.com/llms.txt

The following DataWeave examples show how to convert a file stream into Base64 and to convert a
Base64 string into a file stream. They use a PDF file as the input and output.

MuleSoft uses the standard Base64 encoding as defined in
[RFC 4648, section 4](https://www.rfc-editor.org/rfc/rfc4648#section-4). This standard includes the
`+` and `/` characters and uses `=` for padding. The encoding isn’t URL safe. To use Base64 encoding
in a URL, you must first encode the URL separately.

## Encode a PDF File to Base64

This example performs Base64 encoding on a PDF file. It simulates the transformation in the Read
operation from the [XML configuration](#xml_config) by converting a Base64 file stream
(`application/octet-stream`) to Binary and outputting the Binary in the `text/plain` format.

The example uses the following functions:

- `toBase64` from the `dw::core::Binaries` module to transform a binary value to a Base64 string.

- `readUrl` from the `Core (dw::Core)` module to input a PDF file.

  The example assumes that the PDF file is in the `src/main/resources` directory of a Mule project
  in Anypoint Studio. It provides the name of the PDF file and specifies the MIME type as
  `"application/octet-stream"`. Using `"binary"` as the second `readUrl` argument is also valid for
  this example.

DataWeave Script:

```dataweave
%dw 2.0
import * from dw::core::Binaries
var myPDF = readUrl("classpath://pdf-test.pdf", "application/octet-stream")
output text/plain
---
toBase64(myPDF as Binary)
```

Output (partial results):

```json
JVBERi0xLjYNJeLjz9MNCjM3IDAgb2JqIDw8L0xpbmVhcml6ZWQgMS9MIDIwNTk3L08gNDAvRSAx
...
```

## Decode Base64

This example simulates the transformation in the Write operation from the
[XML configuration](#xml_config) by converting a Base64 file stream (`application/octet-stream`) to
Binary and outputting the Binary in the `application/PDF with binary` format. The example does not
generate a PDF file; a Write operation is required to generate such a file.

The examples use the following functions:

- `fromBase64` in the `dw::core::Binaries` module to transform a Base64 string to a binary value.

- `readUrl` to input a PDF file.

  The example assumes that the PDF file is in the `src/main/resources` directory of a Mule project
  in Anypoint Studio. The function reads the input PDF as an octet-stream MIME type. Using the MIME
  type `"binary"` as the second `readUrl` argument is also valid for this example.

DataWeave Script:

```dataweave
%dw 2.0
import * from dw::core::Binaries
var myPDF = readUrl("classpath://pdf-test.pdf", "application/octet-stream")
var myPDFasBinary = toBase64(myPDF as Binary)
output application/PDF with binary
---
fromBase64(myPDFasBinary as String) as Binary
```

The output is the text representation of the PDF.

Output (partial results):

```json
%PDF-1.6
%����
37 0 obj <</Linearized 1/L 20597/O 40/E 14115/N 1/T 19795/H [ 1005 215]>>
endobj

xref
37 34
0000000016 00000 n
...
```

## XML Configuration Example

In Anypoint Studio, you can create and run a Mule flow that encodes and decodes a PDF file. After
you complete the procedure, the example reads the file from the specified local directory using the
File Connector Read operation. It writes the PDF to the specified directory by using the File
Connector Write operation, and it uses Transform Message components to encode the file to Base64 and
to decode that output from Base64.

1.  Create a Mule project in Anypoint Studio:

    1.  Select **File > New > Mule Project**.

    2.  Enter a name for your Mule project.

    3.  Click **Finish**.

2.  Use **Add Module** in the Mule Palette to add the File Connector:

    1.  In the **Mule Palette** view, click **(X) Search in Exchange**.

    2.  In the **Add Dependencies to Project** window, type `file` in the search field.

    3.  Click **File Connector** in Available modules.

    4.  Click **Add**.

    5.  Click **Finish**.

3.  In the Studio canvas, navigate to the **Configuration XML** tab.

4.  Copy and paste the following example between the `<mule/>` elements of a Mule flow in Anypoint
    Studio:

    ```XML
    <http:listener-config name="HTTP_Listener_config"
                          doc:name="HTTP Listener config" >
        <http:listener-connection
                                host="0.0.0.0"
                                port="8081" />
    </http:listener-config>
    <file:config name="File_Config"
                 doc:name="File Config" >
        <file:connection workingDir="/Users/me/Downloads" />
    </file:config>
    <flow name="encode-decode-base64">
        <http:listener doc:name="Listener"
                     path="/transform"
                     config-ref="HTTP_Listener_config"/>
        <file:read doc:name="Read"
                 config-ref="File_Config"
                 path="pdf-test.pdf" />
        <ee:transform doc:name="Transform Message">
            <ee:message>
              <ee:set-payload><![CDATA[%dw 2.0
    import * from dw::core::Binaries
    output text/plain
    ---
    toBase64(payload as Binary)]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <ee:transform doc:name="Transform Message" >
            <ee:message >
              <ee:set-payload ><![CDATA[%dw 2.0
    import * from dw::core::Binaries
    output application/pdf with binary
    ---
    fromBase64(payload as String) as Binary]]></ee:set-payload>
            </ee:message>
        </ee:transform>
        <file:write doc:name="Write"
                  path="/Users/me/pdf-test-result.pdf"/>
        <set-payload
                  value='#["PDF file created in the directory specified in the Write operation."]'
                  doc:name="Set Payload" />
    </flow>
    ```

    Note that it is valid to replace the directive `output application/pdf with binary` in the
    second Transform Message component with `output application/octet-stream`.

5.  Place a PDF file of your choice into a local directory on your machine.

6.  Correct the input and output directories of the **Read** and **Write** operations:

    - In the **Read** operation, change the **File Path** value \`/Users/me/Downloads" to the
      location of your PDF file.

    - In the **Write** operation, change the **Path** value `/Users/me/pdf-test.pdf"` to the
      location and file name that you want to output the file.

7.  Run the project in Studio.

8.  Load the `0.0.0.0:8081/transform` into a browser.

    This action creates a PDF file in the specified location and generates a separate `transform`
    file with the message in the Set Payload component,
    `PDF file created in the directory specified in the Write operation.`

## See Also

- [Binaries (dw::core::Binaries)](https://docs.mulesoft.com/dataweave/2.6/dw-binaries)

- [DataWeave Selectors](https://docs.mulesoft.com/dataweave/2.6/dataweave-selectors)

- [DataWeave Cookbook](https://docs.mulesoft.com/dataweave/2.6/dataweave-cookbook)
