28

I was wondering...

(except the issue with the base64's plus'+' sign in query string - which is translated to 'space' and can be solved by %2b) :---> which is the preferred way to transfer data in query string?

Both functions can be used through the JS commands:

  • btoa
  • encodeUriComponent

so im asking myself(and you) :

when should I use what ? ( ive always used encodeUriCompoonent - by instinct).

the problem that the definitions are different - but the implementations can be similar...

edit

I think ive found the reason for asking.... ( and why nobody asked it before)

enter image description here

Charles
  • 50,943
  • 13
  • 104
  • 142
Royi Namir
  • 144,742
  • 138
  • 468
  • 792

4 Answers4

18

base64 is used to transfer binary data. (not supported in IE, cant encode spacial chars.)

encodeURIComponent only encodes special characters.

An interesting thing is that you can't apply base64 to unicode strings without encodeURIComponent: https://developer.mozilla.org/en/DOM/window.btoa

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
meze
  • 14,975
  • 4
  • 47
  • 52
  • like i said `the problem that the definitions are different - but the implementations can be similar...` -still didnt get an answer....:) – Royi Namir Apr 22 '12 at 11:47
  • `the implementations can be similar` ? It has different purposes that are described in definitions. – meze Apr 22 '12 at 11:51
  • I can use also **base64** to transfer `userid=1` and also **encodeURIComponent** ... im looking for the difference. – Royi Namir Apr 22 '12 at 11:52
  • Then no difference, really. But if you transfer Unicode data then you're obliged to use encodeURIComponent. And also base64 will take more space comparing to encodeURIComponent. – meze Apr 22 '12 at 11:55
  • 1
    encodeURIComponent() will only take less space if you exclusively pass characters that won't be encoded (i.e. mostly alphanumeric) - anything actually encoded will take far more space than base64 would. e.g. `btoa('::')` becomes `Ojo=` whereas `encodeURIComponent('::')` becomes `%3A%3A` - and baloons upwards with greater character counts. – lucideer Apr 22 '12 at 14:43
  • 1
    @lucideer in any case more general than `btoa('::')` you need to do a 'btoa(encodeURIComponent(realWorldString))' or else btoa won't work. The case you depict is one in which the input to '::' does not even have one character outside plain ASCII. Even U+0100 breaks. As soon as you desire encoding (i.e. more than ASCII), you need encodeURIComponent anyway and the sizes are added up and comparison does not make much sense. – humanityANDpeace Jul 14 '15 at 17:04
  • Your link is a dead link – Woody Feb 19 '21 at 14:33
3

The answer to this is entirely dependent on your server-side application.

'+' is not translated to 'space' by the client - it is auto-translated to 'space' by some server-side apps, largely for backward-compatibility reasons (conversely, some server-side apps will leave '+' as '+' in compliance with RFC3986 ).

As far as the client is concerned - btoa() and encodeURIComponent() (and encodeURI() and escape()) just encode a string of text into different abstracted strings according to different encoding or escaping algorithms - btoa() usually produces the smallest resultant string using base64 encoding but meze's comment re: unicode is worth taking into account here.

The important thing to note is what your server-side application (some ASP.NET-based setup in your case) then uses to decode that string back to it's original form.

Community
  • 1
  • 1
lucideer
  • 3,842
  • 25
  • 31
  • Considering the necessity to use `encodeURIComponent()` for Unicode character > U+00FF anyway, the use case of `btoa()` and hence the suggested advantage of a smaller size, is very limited – humanityANDpeace Jul 14 '15 at 17:07
0

I use Base64 encoding when I want to convert byte array into a string, with ability to convert it back to byte array later.

UrlEncoding when I want a string to be placed safely within url.

Mariusz
  • 908
  • 1
  • 13
  • 28
-1

fwiw, I use base64 whenever I want to transport anything that CAN be unicode, between a server and a client. urlencode doesn't handle all unicode charachters all that well. It quickly gets a mess with all the percentage signs.

so, in short: expecting unicode input/output, always base64 the transportation.

Adergaard
  • 760
  • 10
  • 24
  • I just saw the other answer by user meze. I don't know what he's talking about. I use base64 encoding exactly BECAUSE it handles unicode strings so well. Japanese, Chinese, Cyrillic input daily on a couple of my sites that gets transported this way. – Adergaard Apr 22 '12 at 11:50
  • 1
    Base64 works only with ASCII and encodeURIComponent does work with unicode. Try it in your browser: `window.btoa('✓ à la mode');` – meze Apr 22 '12 at 11:52
  • 1
    aha, well, I have no idea of what btoa does. when I was talking about base64, I meant it as a general thing. I use it from PHP myself to send stuff up to the client. – Adergaard Apr 22 '12 at 12:44