logo
post image

URL Encoding in Deno

A url can be encoded in Deno by either using the encodeURI() method or creating a new URL object. No libraries are required to be imported.

  • Encoding a url with the standard Javascript encodeURI() method.

    let url = 'http://example.com?scope=email profile&url=http://google.com';
    
    // http://example.com/?scope=email%20profile&url=http://google.com
    let encoded_url = encodeURI(url);
    
  • Encoding a url by creating a new URL object.

    let url = 'http://example.com?scope=email profile&url=http://google.com';
    let url_ob = new URL(url);
    
    // http://example.com/?scope=email%20profile&url=http://google.com
    let encoded_url = url_ob.toString();