How to get params for url with whitespace as '%20' instead of '+' in Rails

url with spaces example
how to pass parameter with space in url javascript
handling white spaces in query params
url encode
rest api parameters with spaces
how to avoid space in url
whitespace in get request
http parameters with spaces

If I have this params for adding to the URL

params = { name: 'John Key' }

and use the method to_param:

params.to_param
 => "name=John+Key"

The point is that '+' is not correctly read by the service used and is needed '%20' instead name=John%20Key: When to encode space to plus (+) or %20?

Is there a way to return the params with '%20' without using gsub?

I would recommend just sticking to the use of a gsub, perhaps with a comment to explain the need for such behaviour.

While you could solve the problem by use of URI.escape, it is supposedly deprecated, as it does not fully conform to RFC specs. See here for a great write-up on it.

Hash#to_param is an alias of Hash#to_query, which calls Object#to_query. The simplest example to demonstrate this + vs %20 issue is:

'John Key'.to_query(:name) # => "name=John+Key"

The implementation of Object#to_query is:

def to_query(key)
  "#{CGI.escape(key.to_param)}=#{CGI.escape(to_param.to_s)}"
end

And so, we find that:

CGI.escape("John Key") # => "John+Key"

Hence, this is why I have referenced the differences between CGI.escape and URI.escape.

URL contains whitespace, URLs with whitespace characters are not recommended as they could cause make sure to encode whitespace using "+" or "%20" in the query-string, and� In current Scenario, I am testing the url in apigee only. So, due to the space in query param value, it is unable to reach to the proper method. I have manually encoded the white space in the url, but still no luck. So I have approached a java script policy to encode my url and then use another policy to decode in the response section.

How about

URI.encode 'John Smith'
# => John%20Smith

Handling white spaces in query params, When I query with the url having the query param value with white space, it is giving me bad request error. And I replace the white space with %� URL contains whitespace. This means that the URL in question contains whitespace characters in the path. URLs with whitespace characters are not recommended as they could cause issues when site visitors share or link to the URL, potentially leading to broken links and a loss of potential link equity.

not really.

Suggest using: your_params_hash.to_query.gsub("+", "%20")

How do I encode URL parameters? - Web Tutorials, This Java tutorial describes how to encode URL parameters using the Notice that the spaces have been converted to plus signs, and the comma has been� That's not true. An URL can use spaces. Nothing defines that a space is replaced with a + sign. As you noted, an URL can NOT use spaces. The HTTP request would get screwed over. I'm not sure where the + is defined, though %20 is standard.

Have you tried using uri? e.g.

require 'uri' URI.escape('John Smith')

The result in this example should be John%20Smith

Realized from other post that URI.escape is technically deprecated. Where h stands for hash, and k is the key in the hash, try this:

params.keys.inject({}) {|h, k| h[k] = ERB::Util.url_encode(params[k]); h }

How to encode spaces in curl GET request URL on Linux , How to encode spaces in curl GET request URL on Linux? tagged Bash, Command, Command line, curl, Howto, Linux, Programming, shell,� We often need to get a route parameter so we can make a component dynamic based on that parameter. In this guide, we'll learn how to extract data or a parameter from a URL, including several different approaches.

Expanding on Julie's answer to achieve the string of encoded values rather than a hash.

With the depreciation of URI.encode it is possible to achieve this with ERB::Util.url_encode, however you need to encode just the key and value pairs, not the full parameter, then join them together.


params = { name: 'John Key' }

# not what we want
ERB::Util.url_encode(params) # => "%7B%3Aname%3D%3E%22John%20Key%22%7D"

Here is a (pretty long) command that will work:

# Construct a new object (a string here) and build our params into it correctly.
# k = key of hash. 
# v = value of hash. 
# a = array created in each_with_object.

params.each_with_object([]) { |(k, v), a| a << [ERB::Util.url_encode(k), ERB::Util.url_encode(v)].join('=') }.join('&')

# => "name=John%20Key"

With more params:

params = { name: 'John Key', occupation: 'Web Developer!', 'Bad Key' => 'but works' }

params.each_with_object([]) { |(k, v), a| a << [ERB::Util.url_encode(k), ERB::Util.url_encode(v)].join('=') }.join('&')

# => "name=John%20Key&occupation=Web%20Developer%21&Bad%20Key=but%20works"

compared to Hash.to_query

params.to_query

# => "Bad+Key=but+works&name=John+Key&occupation=Web+Developer%21"

URLEncoding of spaces in query parameters + vs %20, If I create a GET request with a URL that contains query parameters, they will be automatically added in the query parameter list in Katalon. Here is the kind of errors I am getting when file has white spaces: As long as your url fit (you can see them in browser) that would be no problem. Hope I

Space character in url path encoded as `+` instead of `%20` � Issue , Recent versions of Bravado have started encoding space characters in urls as + Make sure spaces are quoted as %20 in path params #279. Whitespace is preserved by the browser. Text will only wrap on line breaks. Acts like the <pre> tag in HTML: Play it » pre-line: Sequences of whitespace will collapse into a single whitespace. Text will wrap when necessary, and on line breaks: Play it » pre-wrap: Whitespace is preserved by the browser. Text will wrap when necessary, and on

Parsing error, the plus sign is replaced by a blank space. + place , Is there a better way to parse a query string and preserve + than using a I don't have control over how the urls I need to parse are encoded� The previous examples demonstrate the logic and technique behind writing valid URL query string parameters, but there are easier, more efficient ways to produce valid, dynamic links. First of all, rather than manually replacing each and every special character with its encoded equivalent, we can use the magical powers of PHP ’s urlencode

How to Write Valid URL Query String Parameters, Keep your web pages valid by encoding your URL query parameters code still won't validate due to the blank spaces in the title parameter. Good Coding Practice. It is good coding practice to avoid the need for URL escape characters. As a rule of thumb, avoid using the special characters above when formulating a URI string (filename), and I recommend using the hyphen (-) instead of the underscore (_) (as all search engines recognize the hyphen as a space separator, but the same is not true for the underscore.

Comments
  • You misread the answers to the question linked. In query plus signs are perfectly valid, treated as spaces and therefore there is no such redundant Rails helper existing.
  • I need the params for a url of typeform. It does not treat well the '+', but it does with '%20'
  • Yes, it works for the value of a param. But in this case I want to get a group of params with '%20'. Like to_param method but not returning the whitespaces with '+' name=John%20Key