Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

encodeURI doesn't escape `equals` - why?

I have an URI like that:

http://client.dev/dap/module/hdfs-web/api/v1.0/clusters/Cluster%201%20-%20CDH4?operation=copy&to=/user/hdfs/year=2016/partial.txt&overwrite=true

I use encodeURI function to escape string. I'm wondering why spaces are encoded with %20 while equals characters are not?

like image 365
Max Koretskyi Avatar asked Oct 24 '25 06:10

Max Koretskyi


1 Answers

encodeURI encodes a full URI, and URIs can contain = characters. For instance, if a user types in a URI, a first step to resolve it would be to call encodeURI on it.

If on the other hand you are the one constructing the URI, and the input just determines one field (for instance a search query, when given E=mc² you want to resolve https://www.google.com/search?q=E%3Dmc%C2%B2), then you are not encoding a full URI, but a URI component. Use encodeURIComponent for that:

> encodeURIComponent('= ')
'%3D%20'
like image 165
phihag Avatar answered Oct 26 '25 19:10

phihag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!