Ruby URI is useful for this. You can create the entire URL programmatically and add request parameters using this class, and it will handle the encoding for you:
require 'uri' uri = URI.parse('http://foo.com') uri.query = URI.encode_www_form( 's' => "Hello there world" ) uri.to_s
Examples are useful:
URI.encode_www_form([["q", "ruby"], ["lang", "en"]]) #=> "q=ruby&lang=en" URI.encode_www_form("q" => "ruby", "lang" => "en") #=> "q=ruby&lang=en" URI.encode_www_form("q" => ["ruby", "perl"], "lang" => "en") #=> "q=ruby&q=perl&lang=en" URI.encode_www_form([["q", "ruby"], ["q", "perl"], ["lang", "en"]]) #=> "q=ruby&q=perl&lang=en"
These links may also be useful:
- When do I need to encode a space to plus (+) or% 20?
- Space character URL encoding: + or% 20?
- 17.13.4 Types of form content from W3 "Forms in HTML documents" guidelines.
the Tin Man Jun 29 '13 at 3:10 2013-06-29 03:10
source share