0

I am using a GET API, currently passing an array as a string:

def fetch_details ids
  url = "#{url}/api/v1/get-info?ids=#{ids.join(',')}"
  response = Net::HTTP.get_response(URI.parse(URI.encode(url)))
  if response.code.to_i == 200
    return Oj.load(response.body)
  else
    return {}
  end
end

On the server-side I am extracting id from this method:

def self.get_details(ids)
    ids = ids.split(",").map {|x| x.gsub( " ", "")}
end

For each id, I want to send an array of UUIDs:

ids = [100,21,301]
uuids= {["abc","bca"],["Xyz"],["pqr","345"]}

Something like this

hash=[
       100=>[abc,bca],
       21=>[xyz],
       301=>[pqr,345]
     ]

The endpoint uses the id and corresponding UUIDs to join two tables in database query so I should be able to extract the id and corresponding UUID at the end.

How do I pass both these values?

12
  • I understand that this is probably psuedocode but from reading this its really questionable if you even understand the difference between arrays and hashes. You should at least make a minimum effort to make the code syntactically correct. Commented Mar 22, 2020 at 18:09
  • hash is also an array. In array, an index is limited to integer. I am looking for a way to send and retrieve data in get API. Commented Mar 22, 2020 at 19:55
  • 1
    I have no idea what you mean here. In Ruby hashes are most definitely not arrays. This is not PHP we are talking about. In Ruby hashes can have numbers as keys. They are still not arrays. Commented Mar 22, 2020 at 19:57
  • 1
    {1,2,3} is a syntax error - not an array Commented Mar 22, 2020 at 20:08
  • 1
    Downvotes aren't used to signify someone used "edit" or similar. They're to signify someone feels the question shows no research, isn't clear or is not useful. Since there are multiple comments telling you that your code isn't correct and needs to be fixed I'd say the question shows no research, and is not useful. Yes, SO wants accuracy because your question is supposed to help others in the future, not just you in the moment. I'd recommend fixing that, making sure the question is asked well, then upvotes might start happening. Commented Mar 23, 2020 at 21:14

1 Answer 1

2

To pass an array in the parameters in Rails/Rack you need to add brackets to the name and repeat the parameter:

/api/v1/get-info?ids[]=1&ids[]=2&ids[]=3

You can use Hash#to_query from ActiveSupport to generate the query string:

irb(main):001:0> { ids: [1,2,3] }.to_query
=> "ids%5B%5D=1&ids%5B%5D=2&ids%5B%5D=3" 

As pointed out by @3limin4t0r you should only use this for one-dimensional arrays of simple values like strings and numbers.

To pass a hash you use brackets but with keys in the brackets:

/api/v1/get-info?foo[bar]=1&foo[baz]=2

Again you can generate the query string with #to_query:

irb(main):002:0> { foo: { bar: 1, baz: 2 } }.to_query
=> "foo%5Bbar%5D=1&foo%5Bbaz%5D=2"

The keys can actually be numbers as well and that should be used to pass complex structures like multidimensional arrays or an array of hashes.

Sign up to request clarification or add additional context in comments.

3 Comments

Worth pointing out might be that's it's wise to only use arrays as last complex object. When nesting hashes inside array's some platforms might not be able to decode the params. Example data {ids: [{id: 1, uuids: [3]}, {id: 2, uuids: [4]}]} will output ids[][id]=1&ids[][uuids][]=3&ids[][id]=2&ids[][uuids][]=4. The parser isn't really able to tell to which element inside the ids array ids[][uuids][]=4 belongs. Instead use {ids: {0 => {id: 1, uuids: [3]}, 1 => {id: 2, uuids: [4]}}} which produces ids[0][id]=1&ids[0][uuids][]=3&ids[0][id]=2&ids[0][uuids][]=4
^ The last output should be ids[0][id]=1&ids[0][uuids][]=3&ids[1][id]=2&ids[1][uuids][]=4 I forgot to change the 0 to 1 for the last two parameters.
Provided you actually fix the syntax errors and the fact that you are URL encoding the entire url and not just the query string you could create a hash with for example { ids: Hash[ids.zip(uuids)] }.to_query. In which case you can get the hash though params[:ids]. If you don't wrap the this hash in a fixed key its going to be really hard to use as you have to iterate though the whole params hash.

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.