Skip to content

Instantly share code, notes, and snippets.

@bgadrian
Last active October 23, 2017 08:57
Show Gist options
  • Save bgadrian/238299bfec60012849b9c7714d6e94b6 to your computer and use it in GitHub Desktop.
Save bgadrian/238299bfec60012849b9c7714d6e94b6 to your computer and use it in GitHub Desktop.
//JavaScript object
const myData={"id":1,"name":"Adrian","info":"My %website% is https://adrian.me"}
//Encode the data into JSON format, as a string
const asText=JSON.stringify(myData)
//encode the JSON string in Base64 for better transportation, like an envelope for a letter
//https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
const asBinary=window.btoa(asText)
//create an URL and encode it
const url=encodeURI("https://httpbin.org/get?user="+asBinary)
console.log(asText) //{"id":1,"name":"Adrian","info":"My %website% is https://adrian.me"}
console.log(asBinary) //eyJpZCI6MSwibmFtZSI6IkFkcmlhbiIsImluZm8iOiJNeSAld2Vic2l0ZSUgaXMgaHR0cHM6Ly9hZHJpYW4ubWUifQ==
console.log(url) //https://httpbin.org/get?user=eyJpZCI6MSwibmFtZSI6IkFkcmlhbiIsImluZm8iOiJNeSAld2Vic2l0ZSUgaXMgaHR0cHM6Ly9hZHJpYW4ubWUifQ==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment