8

I am trying to create http parameters from a hash I have using Ruby on Rails, I have tried using URI.encode_www_form(params) , but this is not generating the parameters correctly.

Below is the hash I have

params['Name'.to_sym] = 'Nia Kun'
params['AddressLine1'.to_sym] = 'Address One'
params['City'.to_sym] = 'City Name'

This method converts space to +, what I want is it to convert space with %20

I am getting "Name=Nia+Kun&AddressLine1=Address+One&City=City+Name" but I need this spaces to be converted to %20

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
opensource-developer
  • 2,826
  • 4
  • 38
  • 88

4 Answers4

6

You could do it like this:

URI.encode_www_form(params).gsub("+", "%20")

if that is really what you need.

See also When to encode space to plus (+) or %20? why it does it in this way.

Community
  • 1
  • 1
IngoAlbers
  • 5,722
  • 6
  • 31
  • 45
2

What you are looking for is URI::escape.

URI::escape "this big string"
=> "this%20big%20string"

EDIT

Bringing it all together:

  • You don't need to convert to symbols for your params, rails is smart and knows about with_indifferent_access. Strings and symbols will both work.
  • Your code would look like this:

.

name = params['Name']# = 'Nia Kun'
address_one = params['AddressLine1']# = 'Address One'
city = params['City']# = 'City Name'

URI::encode "http://www.example.com?name=#{name}&address_one=#{address_one}&city=#{city}"

#=> "http://www.example.com?name=Nia%20Kun&address_one=Address%20One&city=City%20Name"
Max Alcala
  • 781
  • 6
  • 17
  • where in the above code do i need to use URI::escape, i just want space to ve converted to %20 not entire URL – opensource-developer Sep 29 '15 at 14:39
  • Hello max, its not working and converting every spacial character in url to %20 or %40 – opensource-developer Sep 29 '15 at 14:58
  • I'd be curious as to what characters you are looking to escape/not escape and why? URI::encode (or its alias, [URI::escape](http://ruby-doc.org/stdlib-2.1.2/libdoc/uri/rdoc/URI/Escape.html)) precisely replaces all unsafe chars with codes. If you are not sure why something works, there is a bunch of reference out there - wikipedia has a [great article](https://en.wikipedia.org/wiki/Percent-encoding#Percent-encoding_in_a_URI) on it. – Max Alcala Sep 29 '15 at 15:10
1

You can write custom method. Something like this:

p = {x: 'some word', y: 'hello there'}
URI.encode p.to_a.map {|inner| inner.join('=') }.join('&')
# "x=some%20word&y=hello%20there"

So basically you flatten params to array of array, then transform them to url string, then encode it.

EDIT:

Final solution will look like this:

def encode_url(params)
  URI.encode params.to_a.map {|inner| inner.join('=')}.join('&')
end

encode_url(my_params)
vladra
  • 186
  • 9
0

You can use GSUB:

myString.gsub(" ", "%20")

Quoting the doc:

This method doesn't convert *, -, ., 0-9, A-Z, _, a-z, but does convert SP (ASCII space) to + and converts others to %XX.

Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142