3

I seem to be having the same problem as this chap here

I want to encode some parameters (for the import.io api). Effectively:

params = {
  :input => "webpage/url:http://www.example.com",
  :input => "keywords:some+keywords"

}

But that won't work, so I think this is the right approach:

params = { :input => ["webpage/url:http://www.example.com", "keywords:some+keywords"] }

and I want it to output

params.to_query
=> "input=webpage%2Furl%3Ahttp%3A%2F%2Fwww.example.com%2Fsome-id&input=keywords%3Asome%2Bkeywords"

unfortunately, I get

"input%5B%5D=webpage%2Furl%3Ahttp%3A%2F%2Fwww.example.com%2Fsome-id&input%5B%5D=keywords%3Asome%2Bkeywords"

It's adding [] after the input, which I believe is standard behaviour. How can I stop it doing it?

To clarify, what is the ruby or 'rails way' of dealing with url parameters that require duplicate keys?

8
  • Why do you want to stop it from doing it? That's a valid query param Commented Jan 15, 2016 at 14:26
  • 1
    [] is how arrays are handled in params. If you want the end result to be the same as the start params you'll need those [] Commented Jan 15, 2016 at 14:28
  • The issue is that I can't call both of them "input" as the hash will just overwrite itself.. I need a way of passing to values, both with the key "input" to a set of query params that I'm going to pass into a URL. Commented Jan 15, 2016 at 15:59
  • Anthony... I'm sure it is, but it's breaking the api. i.e. the api won't accept that as valid parameters. It is perfectly happy to accept variables with duplicate keys. Not my API so not something I can do anything about, other than to follow their rules.... Commented Jan 15, 2016 at 16:00
  • Yes, input should be sent as an array of values. That's how we send multiple values. If your API doesn't accept it then it's pretty stupid. Are you sure that is the case? We can suggest hacky workarounds, but not knowing anything about the API it's hard to advise. Commented Jan 15, 2016 at 16:05

2 Answers 2

3

Ran into a similar issue, there's a helpful post here Ruby Hash with duplicate keys? but briefly

params = {}.compare_by_identity
params['input'] = "webpage/url:http://www.example.com"
params['input'.dup] = "keywords:some+keywords"

then

params.to_query

returns

"input=keywords%3Asome%2Bkeywords&input=webpage%2Furl%3Ahttp%3A%2F%2Fwww.example.com"
Sign up to request clarification or add additional context in comments.

Comments

1

Some characters in a url have special importance to the processing of the url: they are reserved, like keywords in a programming language. See Which characters make a URL invalid?

If you try to use these as the name or value of a parameter, it will break the uri and you'll get hard to predict results like you're seeing.

The answer is to URI escape the string, which will replace special characters with their encoded version. Rails will automatically unescape them when it gets the the request, so you don't need to worry about it.

You can escape them manually, but the best way, if you have them as a hash already, is to call .to_param on the hash.

params = { :input => ["webpage/url:http://www.example.com", "keywords:some+keywords"] }
=> {:input=>["webpage/url:http://www.example.com", "keywords:some+keywords"]}
params.to_param
=> "input%5B%5D=webpage%2Furl%3Ahttp%3A%2F%2Fwww.example.com&input%5B%5D=keywords%3Asome%2Bkeywords"

2 Comments

Yeah, i think i misunderstood what he was asking.
As I think you've realised, the issue's not with the paramters being encoded. That's fine. It's with the array params being added after input in the variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.