# Base64URL

**URL:** <https://forum.xojo.com/t/base64url/40718>\
**Category:** General\
**Created:** [November 18, 2017, 6:14am UTC](https://forum.xojo.com/t/base64url/40718 "2017-11-18T06:14:41Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [November 18, 2017, 6:14am UTC](https://forum.xojo.com/t/base64url/40718/1 "2017-11-18T06:14:41Z")

</div>

There is a variation of Base64 called Base64URL that’s essentially Base64 with tweaks to allow encoded strings in URLs without conflict. The following code will easily let you encode and decode this:

```auto
Public Shared Function EncodeBase64URL(value As String) as String
  if value = "" then
    return ""
  end if
  
  dim result as string = EncodeBase64( value )
  
  result = ReplaceLineEndings( result, "" )
  result = result.ReplaceAllB( "+", "-" )
  result = result.ReplaceAllB( "/", "_" )
  result = result.ReplaceAllB( "=", "" )
  
  return result.DefineEncoding( Encodings.UTF8 )
End Function

Public Shared Function DecodeBase64URL(value As String) as String
  if value = "" then
    return ""
  end if
  
  value = value.ReplaceAllB( "-", "+" )
  value = value.ReplaceAllB( "_", "/" )
  
  //
  // The native decoder doesn't care about the padding ("=") so
  // we don't have to bother putting it back in
  //
  
  dim result as string = DecodeBase64( value )
  return result
End Function
```

Enjoy.

---

<div class="post-metadata">

**Author:** ![Emile\_Schwarz](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/90ced4/32.png) [@Emile\_Schwarz](https://forum.xojo.com/u/Emile_Schwarz)\
**Post date:** [November 18, 2017, 7:43am UTC](https://forum.xojo.com/t/base64url/40718/2 "2017-11-18T07:43:25Z")

</div>

Hi Kem,

Thank you for the sharing.

What about an URL in this format:

```auto
https://safr.kingfeatures.com/idn/ck3/content.php?file=
```

Nota: I removed the part after `?file=` (this was an encoded string, useless here).

Also: I may have missed the point. If so, sorry.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [November 18, 2017, 2:42pm UTC](https://forum.xojo.com/t/base64url/40718/3 "2017-11-18T14:42:02Z")

</div>

If you want to include binary or even long text data in a url, it has to be encoded somehow. You can encode individual bytes (%01 for cntl-A or + for a space, for example), hex-encode the data, or use Base64URL. You can’t use regular Base64 because it 1) includes line breaks, and 2) may include characters that have special meaning in a url like “/”.

See

[https://brockallen.com/2014/10/17/base64url-encoding/](https://brockallen.com/2014/10/17/base64url-encoding/)

---

<div class="post-metadata">

**Author:** ![anon93744516](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anon93744516](https://forum.xojo.com/u/anon93744516)\
**Post date:** [November 18, 2017, 6:20pm UTC](https://forum.xojo.com/t/base64url/40718/4 "2017-11-18T18:20:09Z")

</div>

Wouldn’t you just use the [EncodeURLComponent()](http://developer.xojo.com/encodeurlcomponent) for your Base64 value?

Encoding URL information, to be used in either HTTP GET or POST requests, applies to the “value” of an argument, where that value may contain URL reserved characters like /, =, + and spaces. The encoding function shouldn’t care whether the value is a Base64 string or just some other string type value.

In JavaScript, there is a similar function called [encodeURIComponent](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent), which would be used like the following:

```auto
var url = 'http://example.com/redirectTo?target=' + encodeURIComponent('http://example.org/') 
```

It’s the value of “target” that needs to be URL encoded. Which would result in a value like http%3a%2f%[2fexample.org](http://2fexample.org)%2f.

I’m sure constructing a URL “string” in Xojo to contain your Base64 information would be similar, except using the EncodeURLComponent() method of course. Something like:

```auto
Dim url As String = 'http://example.com/redirectTo?target=' + EncodeURLComponent(myBase64StringVar) 
```

Note: Base64 strings tend to be very long (depending on your content) and there are limits to how long a URL can be, before browsers, proxies or other network points will fail to handle the request.

The upper limit to the length of URL is difficult to gauge, because it depends on many factors, but I would put that limit at about 2,000 characters. Your mileage may vary.

I hope that helps, but my apologies if I misunderstood what you’re trying to do.

Edit: I realize you’re offering an alternative solution @Kem Tekinay, but I guess my first thought was about whether you’re controlling both ends of the URL (the sender and the recipient). Because both would require using your custom function. Whereas sending with an RFC standards compliant URL encoding method would allow the recipient to decode the value using the corresponding standards compliant method as well (where the recipient may be using a different platform than Xojo).

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [November 18, 2017, 7:35pm UTC](https://forum.xojo.com/t/base64url/40718/5 "2017-11-18T19:35:23Z")

</div>

Base64URL is rfc compliant, used in, for example, a json web token. I merely offered code that will make dealing with it easier.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [November 18, 2017, 7:36pm UTC](https://forum.xojo.com/t/base64url/40718/6 "2017-11-18T19:36:38Z")

</div>

I should add, until just the other day, I hadn’t heard of it either. 🙂

---

<div class="post-metadata">

**Author:** ![anon93744516](https://forum.xojo.com/letter_avatar_proxy/v4/letter/a/5e9695/32.png) [@anon93744516](https://forum.xojo.com/u/anon93744516)\
**Post date:** [November 18, 2017, 7:55pm UTC](https://forum.xojo.com/t/base64url/40718/7 "2017-11-18T19:55:07Z")

</div>

> [@360304:@Kem Tekinay](#):
>
> Base64URL is rfc compliant, used in, for example, a json web token. I merely offered code that will make dealing with it easier.

My bad, I didn’t read the link you gave later. I thought this was just about generic URL encoding.

Yes, you’re quite right Kem, JWT tokens are the exception case (RFC 4648). I didn’t get that part. My apologies.

In which case, nicely done. Thank you.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [November 18, 2017, 9:18pm UTC](https://forum.xojo.com/t/base64url/40718/8 "2017-11-18T21:18:21Z")

</div>

I forgot about the lineWrap parameter in EncodeBase64 so this code is better:

```auto
Public Shared Function EncodeBase64URL(value As String) as String
  if value = "" then
    return ""
  end if
  
  dim result as string = EncodeBase64( value, 0 )
  
  result = result.ReplaceAllB( "+", "-" )
  result = result.ReplaceAllB( "/", "_" )
  if result.RightB( 2 ) = "==" then
    result = result.LeftB( result.LenB - 2 )
  elseif result.RightB( 1 ) = "=" then
    result = result.LeftB( result.LenB - 1 )
  end if
  
  return result.DefineEncoding( Encodings.UTF8 )
End Function
```

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [October 15, 2020, 1:46pm UTC](https://forum.xojo.com/t/base64url/40718/9 "2020-10-15T13:46:19Z")

</div>

@Kem_Tekinay did you happen to make a Base64URL for iOS? Is it as easy as taking this code and changing the Strings to Text and I assume how we handle the ReplaceLineEndings etc.?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 15, 2020, 2:14pm UTC](https://forum.xojo.com/t/base64url/40718/10 "2020-10-15T14:14:32Z")

</div>

This relies on EncodeBase64 which, if memory serves, does not exist in iOS (yet), but does live in my M\_Text module.

> **[MacTechnologies Consulting (914) 500-8878](http://www.mactechnologies.com/index.php?page=downloads#m_text)**

It should be easy to adapt the code to Text through that.

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [October 15, 2020, 2:39pm UTC](https://forum.xojo.com/t/base64url/40718/11 "2020-10-15T14:39:14Z")

</div>

You are correct sir but I do have declares for encode/decode64.

Thanks Kem!

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [October 15, 2020, 5:02pm UTC](https://forum.xojo.com/t/base64url/40718/12 "2020-10-15T17:02:46Z")

</div>

I also have your M\_Text module which has both as you know 😉

---

<div class="post-metadata">

**Author:** ![Mike\_Cotrone](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_cotrone/32/19940_2.png) [@Mike\_Cotrone](https://forum.xojo.com/u/Mike_Cotrone)\
**Post date:** [October 15, 2020, 8:49pm UTC](https://forum.xojo.com/t/base64url/40718/13 "2020-10-15T20:49:04Z")

</div>

Ill post my work on this in the iOS channel later on. Thanks @Kem_Tekinay

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [October 29, 2020, 6:59pm UTC](https://forum.xojo.com/t/base64url/40718/14 "2020-10-29T18:59:49Z")

</div>


