You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
text: "Hi Alex, I am trying to encode pdf containing some text and images/logos to base 64. I need to send endoded base 64 content to external system. I tried to encode pdf with `tobase()` function in dataweave and using the java function which you have given in your blog. But base64 content generated using these two ways is not correct. When external system is generating pdf from base 64 content , the resulting pdf is showing error as `Message: Illegal character: 41` . Basically images/logos in PDF are not loading properly When I send base 64 content generated using online tool (https://base64.guru/converter/encode/pdf). then external system is able to generate PDF properly from it."
16
+
- author: Alex Martinez
17
+
date: 2023-05-10
18
+
replyTo: wella test
19
+
text: |
20
+
That is so interesting! I have never tried it with pdfs. If you think this is a bug, feel free to raise an issue here: https://github.com/mulesoft-labs/data-weave-rfc
21
+
22
+
You can also ask a question to the wider community using Stack Overflow with the DataWeave tag: https://stackoverflow.com/questions/tagged/dataweave?sort=newest
23
+
- author: tanushreemazumdar08
24
+
date: 2021-02-20
25
+
parts:
26
+
- text: |
27
+
Hi Alexendra
28
+
29
+
I have recieved your response via email so writing again. I am receiving below payload from an external system :
My goal is to convert this to json. For this I did the same scripting tool->groovy script execution by feeding java input of this payload. I am receiving below error when posting from postman:
35
+
`java.lang.IllegalArgumentException: Illegal base64 character 25`
36
+
37
+
Please let me know how can I fix this.
38
+
39
+
Thank Yiu
40
+
Tanushree
41
+
- author: Alex Martinez
42
+
date: 2021-03-11
43
+
replyTo: tanushreemazumdar08
44
+
parts:
45
+
- text: |
46
+
Hi @Tanushree!
47
+
48
+
Your string is not a Base64 encoded payload, it's a URI component string.
49
+
50
+
To convert that string into a JSON format, you can use this code:
51
+
- lang: dataweave
52
+
code: |
53
+
%dw 2.0
54
+
output application/json
55
+
import * from dw::core::URL
56
+
---
57
+
read(decodeURIComponent(payload))
58
+
- text: |
59
+
You can read more about the URL module here: https://docs.mulesoft.com/mule-runtime/4.3/dw-url
60
+
and the read function here: https://docs.mulesoft.com/mule-runtime/4.3/dw-core-functions-read
61
+
62
+
Why is it not a base64 encoded string?
63
+
64
+
The error message mentions a character 25, which is a percentage (%) character (from Hex: https://ascii.cl/).
65
+
66
+
If you take a look at the Base64 alphabet in this blog post, for both regular and URL encoding, none of them include the percentage character. For this same reason, it wouldn't be able to decode a string that contains a percentage character (% - 25 in Hex).
67
+
68
+
This is why you get the `illegal base64 character 25` error - it doesn't exist in its alphabet!
69
+
70
+
Also, if you try to encode any string into a base64 encoding, you'll notice two things:
71
+
1) The base64 encoded string contains a final double equals sign (`==`), which your string doesn't contain.
72
+
2) The base64 encoded string is made of (what seems to be) random characters (like `cHJvc3RkZXY_YmxvZw==`), or not easily legible by a human. While your string can be read by a human, for example, right at the start of your string it contains `%7B%22type%22%3A%22block_actions%22` - which can be read by a human.
73
+
74
+
You can confirm these 2 points by testing with some Java code directly at https://www.tutorialspoint.com/compile_java_online.php
text: "Thanks for this post @Alex, for URL encoding do we need to use Java? Is there any option to generate byte array from a string payload? If yes, then it seems we can use `Base64.getUrlencoder().encode(payload)`"
92
+
- author: Alex Martinez
93
+
date: 2021-03-11
94
+
replyTo: Kishan Mohan
95
+
text: "Hi Kishan! No, you don't need to use Java. This was just an example that's using Java. And, yes! You can use that function to encode inside the Groovy script :) I didn't use it in this example because the problem was just to decode what was received from the server."
12
96
---
13
97
14
98
Are you familiar with the fromBase64 or the toBase64 functions from DataWeave 2.0? What about the getUrlEncoder or the getEncoder functions from Java? Do you know the differences between the “basic” Base 64 encoding and the “URL and Filename safe” Base 64 encoding? Well, you may have guessed it by now, but you’re about to find out the answers to these questions!
0 commit comments