Last active
October 23, 2017 08:57
-
-
Save bgadrian/238299bfec60012849b9c7714d6e94b6 to your computer and use it in GitHub Desktop.
Json in Base64 in URL for https://coder.today/encoding-in-web-development-why-how-url-json-base64-beyond-12561b16b1fc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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