I would like to take out a parameter from a URL by its name without knowing which parameter it is, and reassemble the URL again.
I guess it is not that hard to write something on my own using CGI or URI, but I imagine such functionality exists already. Any suggestions?
In:
http://example.com/path?param1=one¶m2=2¶m3=something3
Out:
http://example.com/path?param2=2¶m3=something3
Using &url='+encodeURIComponent(url); to pass a URL from browser to server will encode the url but when it is decoded at the server, the parameters of url are interpreted as seperate parameters and not as part of the single url parameter.
You cannot hide parameters. Even if you use the post method instead of the get method to remove parameters from the url. You can still see the passed parameters in the request message. The way to safely hide parameters is to encrypt them.
I prefer to use:
require 'addressable/uri'
uri = Addressable::URI.parse('http://example.com/path?param1=one¶m2=2¶m3=something3')
params = uri.query_values #=> {"param1"=>"one", "param2"=>"2", "param3"=>"something3"}
params.delete('param1') #=> "one"
uri.query_values = params #=> {"param2"=>"2", "param3"=>"something3"}
uri.to_s #=> "http://example.com/path?param2=2¶m3=something3"
Maybe a little off-topic, but for anyone who's attempting to do this in the context of a rails app you can simply do:
url_for(params.except(:name_of_param_to_delete))
N.B. Tested in rails v2.3.9.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With