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?

code0079
  • 147
  • 3
  • 13
  • 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. – max Mar 22 '20 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. – code0079 Mar 22 '20 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. – max Mar 22 '20 at 19:57
  • https://stackoverflow.com/questions/6097637/whats-the-difference-between-arrays-and-hashes I read this answer. – code0079 Mar 22 '20 at 20:00
  • You don't seem to understand it though. What you are looking for is a hash like `{ 100 => [5,6,7], 21 => [5,6,7] }`. That's a hash - the keys are numbers and the values are arrays of numbers. – max Mar 22 '20 at 20:03
  • 1
    `{1,2,3}` is a syntax error - not an array – max Mar 22 '20 at 20:08
  • you are right. I made a mistake. – code0079 Mar 22 '20 at 20:12
  • The question needs to be updated with syntactically correct Ruby, otherwise it'll be closed. `hash=[` is not how to define a hash, `uuids= {["abc","bca"],["Xyz"],["pqr","345"]}` is attempting to define a hash but will fail. – the Tin Man Mar 22 '20 at 23:16
  • @theTinMan updated. – code0079 Mar 23 '20 at 04:11
  • It's not necessary to tell us you updated it; SO tracks changes and we can look to see what and when something changed. – the Tin Man Mar 23 '20 at 04:31
  • @theTinMan wasn't aware of that. Nice that you informed me. If SO has informed you then could you remove the downvote? – code0079 Mar 23 '20 at 06:41
  • 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. – the Tin Man Mar 23 '20 at 21:14

1 Answers1

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.

max
  • 96,212
  • 14
  • 104
  • 165
  • 2
    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` – 3limin4t0r Mar 22 '20 at 18:16
  • ^ 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. – 3limin4t0r Mar 22 '20 at 18:25
  • 1
    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. – max Mar 22 '20 at 20:31