ywenbo
ywenbo

Reputation: 3091

When should I escape urls?

I have a URL and escaped it using:

url = "http://ec4.images-xxx.com/images/I/41-%2B6wMiewL._SL135_.jpg"
url = URI.escape(url)
puts url => "http://ec4.images-xxx.com/images/I/41-%252B6wMiewL._SL135_.jpg"

From the result I can see that URI escaped the previously escaped %2B again which became %252B, which is not correct.

I want to know how to make sure when one URL should be escaped. Or, is there a smart method that knows when to escape and when not to escape?

Upvotes: 0

Views: 772

Answers (1)

sflinter
sflinter

Reputation: 424

Your first string is already properly URI encoded, so when you try to re-encode it, the URI.escape method is encoding the '%' with '%25' (URI encoding for '+').

If you're really not sure whether your string has been URI encoded or not, you could try to decode it first, and compare it with the original. If they're the same, then it hasn't been encoded.

Upvotes: 1

Related Questions