New topic Closed topic
avatar image
1
Base64 and URL encoding
By Created

I was having an issue where sending a base64 encoded string to the Betty Blocks API would not decode properly. This is due to the way my string was build and encoded. This is not applicable to everyone but it was something that took a while to figure out. Hopefully this information will help someone in the future.

In my case I was sending the information (with C# code) UTF8 encoded. This means that certain characters get encoded. In my case all my plus sign characters '+' got encoded to a space character ' '. When decoding this base64 decoded  file was (ofcourse) corrupt. 

To fix this you'll need to URL-encode your base64 string. This will encode your '+' sign to '%2b'. The Betty Blocks API gets this and knows  that '%2b' is '+'. This will result in a correctly decoded base64.

In C# this was very easy:

//create byte array from file
string path = "path to file";
byte[] bytes = File.ReadAllBytes(path);
//create base64 based on the byte array
string base64_string = Convert.ToBase64String(bytes);
//url encode the base64 string
base64_string = System.Web.HttpUtility.UrlEncode(base64_string);

In short:if you want to send base64 to Betty, remember to URL encode your base64 string so it does not get malformed.





I was having an issue where sending a base64 encoded string to the Betty Blocks API would not decode properly. This is due to the way my string was build and encoded. This is not applicable to everyone but it was something that took a while to figure out. Hopefully this information will help someone in the future.

In my case I was sending the information (with C# code) UTF8 encoded. This means that certain characters get encoded. In my case all my plus sign characters '+' got encoded to a space character ' '. When decoding this base64 decoded  file was (ofcourse) corrupt. 

To fix this you'll need to URL-encode your base64 string. This will encode your '+' sign to '%2b'. The Betty Blocks API gets this and knows  that '%2b' is '+'. This will result in a correctly decoded base64.

In C# this was very easy:

//create byte array from file
string path = "path to file";
byte[] bytes = File.ReadAllBytes(path);
//create base64 based on the byte array
string base64_string = Convert.ToBase64String(bytes);
//url encode the base64 string
base64_string = System.Web.HttpUtility.UrlEncode(base64_string);

In short:if you want to send base64 to Betty, remember to URL encode your base64 string so it does not get malformed.




Answers
Sort by:

This topic is closed.