Skip to content

Instantly share code, notes, and snippets.

@sonnyp
Last active October 27, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sonnyp/c06741eabfddfbdd18d322a90792b147 to your computer and use it in GitHub Desktop.
Save sonnyp/c06741eabfddfbdd18d322a90792b147 to your computer and use it in GitHub Desktop.
function fixedEncodeURIComponent(str) {
return encodeURIComponent(str).replace(/[!'()*]/g, (c) => {
return `%${c.charCodeAt(0).toString(16)}`;
});
}
function urlEncode(params) {
let data = '';
Object.keys(params).forEach((key, idx, { length }) => {
const k = fixedEncodeURIComponent(key.toString());
const v = fixedEncodeURIComponent(params[key].toString());
data += `${k}=${v}`;
if (idx < length - 1) data += '&';
});
// in application/x-www-form-urlencoded spaces are replaced with +
return data.replace(/%20/g, '+');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment