url-Encode vs Base64 encoding ( usages)?

Royi Namir picture Royi Namir · Apr 22, 2012 · Viewed 27.3k times · Source

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

Answer

meze picture meze · Apr 22, 2012

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