Strange Base64 encode/decode problem
Whatever populates params
expects the request to be a URL-encoded form (specifically, application/x-www-form-urlencoded
, where "+" means space), but you didn't URL-encode it. I don't know what functions your language provides, but in pseudo code, queryString
should be constructed from
concat(uri_escape("data"), "=", uri_escape(base64_encode(rawBytes)))
which simplifies to
concat("data=", uri_escape(base64_encode(rawBytes)))
The "+
" characters will be replaced with "%2B
".
You have to use a special base64encode which is also url-safe. The problem is that standard base64encode includes +
, /
and =
characters which are replaced by the percent-encoded version.
http://en.wikipedia.org/wiki/Base64#URL_applications
I'm using the following code in php:
/**
* Custom base64 encoding. Replace unsafe url chars
*
* @param string $val
* @return string
*/
static function base64_url_encode($val) {
return strtr(base64_encode($val), '+/=', '-_,');
}
/**
* Custom base64 decode. Replace custom url safe values with normal
* base64 characters before decoding.
*
* @param string $val
* @return string
*/
static function base64_url_decode($val) {
return base64_decode(strtr($val, '-_,', '+/='));
}