Reputation: 5832
I have an URI object.
require 'uri'
uri = URI.parse 'http://example.com'
I need to merge some path to this URI.
path = URI.escape('/?foo[123]=5')
uri.merge!(path)
And getting exception:
URI::InvalidURIError: bad URI(is not URI?): /?foo[123]=5
This is because some symbols in path considered as unsafe ("[" and "]"), but those symbols ware not escaped by URI.escape.
I'm solving this issue using two URI.escape calls:
path = URI.escape(URI.escape('/?foo[123]=5'), '[]')
uri.merge!(path)
The question is: why URI.escape does not performs that escaping by default?
Or maybe there is some better way to do that?
Upvotes: 3
Views: 1194
Reputation: 23990
Use CGI.escape
instead of URI.escape
:
require 'uri'
require 'cgi'
uri = URI.parse 'http://example.com'
path = CGI.escape('/?foo[123]=5')
uri.merge!(path) #=> #<URI::HTTP:0x00000002947918 URL:http://example.com/%2F%3Ffoo%5B123%5D%3D5>
Upvotes: 1