How to URL encode a string in Ruby
str = "\x12\x34\x56\x78\x9a\xbc\xde\xf1\x23\x45\x67\x89\xab\xcd\xef\x12\x34\x56\x78\x9a".force_encoding('ASCII-8BIT')
puts CGI.escape str
=> "%124Vx%9A%BC%DE%F1%23Eg%89%AB%CD%EF%124Vx%9A"
Ruby - URL encoding
What about CGI::escape
You need to only encode the parameters though.
url = "http://xyz.com/hello?"
params = "name=john&msg=hello\nJohn\n\rgoodmorning¬e=last\night I went to \roger"
puts "#{url}#{CGI::escape(params)}"
# => "http://xyz.com/hello?name%3Djohn%26msg%3Dhello%0AJohn%0A%0Dgoodmorning%26note%3Dlast%0Aight+I+went+to+%0Doger"
Parsing string to add to URL-encoded URL
In 2019, URI.encode is obsolete and should not be used.
require 'uri'
URI.encode("Hello there world")
#=> "Hello%20there%20world"
URI.encode("hello there: world, how are you")
#=> "hello%20there:%20world,%20how%20are%20you"
URI.decode("Hello%20there%20world")
#=> "Hello there world"
Simple cipher to encode URL
Use Base64 urlsafe_encode64
Base64 is a good idea to use, Ruby provides URL safe implementation of it, because the default will produce characters like + and =. urlsafe_encode64
will use '-' instead of '+' and '_' instead of '+' so they can be passed like URL parameters.
require "base64"
encoded_url = Base64.urlsafe_encode64('http://www.someurl.com', ) #=> "aHR0cDovL3d3dy5zb21ldXJsLmNvbQ=="
decoded_url = Base64.urlsafe_decode64(encoded_url) #=> "http://www.someurl.com"
Ruby equivalent to PHPs urlencode
Ruby uses UTF-8 strings by default:
str = 'ö'
str.encoding
#=> #<Encoding:UTF-8>
If you want an ISO 8859 encoded string in Ruby, you have to convert it:
str.encode('ISO-8859-1')
#=> "\xF6"
to URL-encode a string, there's CGI.escape
:
require 'cgi'
CGI.escape(str.encode('ISO-8859-1'))
#=> "%F6"
to encode an URL, use URI.escape
:
require 'uri'
url = 'http://example.com/this/is/an/ö'
URI.escape(url.encode('ISO-8859-1'))
#=> "http://example.com/this/is/an/%F6"
Is there a function to url encode dot ('.') in ruby
You actually don't need to encode the dot. After the ?
in the url, /
and .
don't have any specific meaning.
Converting url encoded strings into plain text with ruby
You can use CGI::unescape
for URL-decoding:
require 'cgi'
string = CGI::unescape("1+%2B+2+%3D+3")
#=> "1 + 2 = 3"
Turning a json string into a URL encoded string (Rails)
Sure you can use the uri
library shown here
[2] pry(main)> require 'uri'
=> true
[3] pry(main)> URI.encode('{"email":"name@gmail.com"}')
=> "%7B%22email%22:%22name@gmail.com%22%7D"
Ruby - how to encode URL without re-encoding already encoded characters
I can't think of a way to do this that isn't a little bit of a kludge. So I propose a little bit of a kludge.
URI.escape
appears to work the way you want in all cases except when characters are already encoded. With that in mind we can take the result of URI.encode
and use String#gsub
to "un-encode" only those characters.
The below regular expression looks for %25
(an encoded %
) followed by two hex digits, turning e.g. %252f
back into %2f
:
require "uri"
DOUBLE_ESCAPED_EXPR = /%25([0-9a-f]{2})/i
def escape_uri(uri)
URI.encode(uri).gsub(DOUBLE_ESCAPED_EXPR, '%\1')
end
puts escape_uri("https://www.example.com/url-déjà-vu")
# => https://www.example.com/url-d%C3%A9j%C3%A0-vu
puts escape_uri("https://somesite.com/page?stuff=stuff&%20")
# => https://somesite.com/page?stuff=stuff&%20
puts escape_uri("http://example.com/a%2fb")
# => http://example.com/a%2fb
I don't promise that this is foolproof, but hopefully it helps.
url encode equivalent in ruby on rails
I believe the u
helper method is what you're looking for:
<%=u "URL ENCODE ME
" %>
I can't seem to find the documentation for that method, but if I find it in the near future I'll be sure to put a link in here.
Edit: You can find the documentation for this method here.
Related Topics
Can Someone Explain the Class.Superclass.Class.Superclass Paradox
How to Check If a Given Directory Exists in Ruby
Differencebetween Send_Data and Send_File in Ruby on Rails
How to Have Multiple Versions of Ruby and Rails, and Their Combinations on Windows
Differencebetween Class and Klass in Ruby
Ruby on Rails Plural (Controller) and Singular (Model) Convention - Explanation
Ruby on Rails: How to Run Things in the Background
Ruby Craziness: Class VS Object
How to Install Ruby 2 on Ubuntu Without Rvm
Passing Multiple Error Classes to Ruby's Rescue Clause in a Dry Fashion
Forking a Gem for a Rails Project
How to Check for File Existence
Parsing Xls and Xlsx (Ms Excel) Files with Ruby
Yaml How Many Spaces Per Indent
Given an Array of Arguments, How to Send Those Arguments to a Particular Function in Ruby
How to Generate Zip File Without Saving to the Disk with Ruby