sharptooth
sharptooth

Reputation: 170549

How do I percent-escape a Base64 string in Ruby?

In a Ruby program I have a password that is a Base64 string and so can contain forward slashes. I use that password and the username to perform HTTP requests

username = "User"
password = "/Base/64/With/Slashes"
requestUrl = "http://#{username}:#{password}@company.com"
response = RestClient.get(requestUrl)

so if password happens to contain forward-slashes those will be interpreted as port of the URI and I'll have an error message saying I have an invalid URI. Clearly each forward slash inside password must be replaced with %2F.

I tried to use URI.escape(), but it doesn't affect forward slashes.

How do I percent-escape Base64 string so that the result can be used for HTTP requests authentication?

Upvotes: 0

Views: 641

Answers (1)

Darshan Rivka Whittle
Darshan Rivka Whittle

Reputation: 34061

@injekt has the right answer in a comment, but here's a slightly elaborated version as an answer:

CGI.escape() is an appropriate tool for that task. Its documentation:

URL-encode a string.

Upvotes: 1

Related Questions