0

I am a bit puzzled by this myself. If for some reasons I would like to encode any valid url into valid pathname, say, to be used on an image proxy with an url structure like:

http://image-proxy/hash_of_url/encoded_url

Is there an efficient way to achieve encoded_url in javascript? I am on node.js runtime.

I need to do this because it would be used by another proxy not in my control, which had problem with an url like this:

http://image-proxy/hash_of_url/?url=encoded_url

PS: hash_of_url is a hmac digest of original url, to ensure url is generated by server, but that's not important to my question.

bitinn
  • 9,188
  • 10
  • 38
  • 64
  • You mean `encodeURIComponent`? – Andrew Lavers May 21 '15 at 15:59
  • Is `encodeURIComponent` safe for pathname? I use it for my 2nd example for sure, but can we be sure `encodeURIComponent` encode the url to be a valid segment in pathname? – bitinn May 21 '15 at 16:13
  • And `encodeURI` seem like a better fit than `encodeURIComponent` but my question remain, are they the best options? – bitinn May 21 '15 at 16:17

1 Answers1

0

If you want to insert your encoded URL into a path you should use encodeURIComponent which will encode path boundary characters like /, ?, & and so on.

http://www.w3schools.com/jsref/jsref_encodeURI.asp

Andrew Lavers
  • 8,023
  • 1
  • 33
  • 50