2

I want to do a GET request like that using the standard Ruby client net/http:

stores/?ids=2,24,13

I'm trying to do it this way, where store_ids is an array of ids, but it is not working. If I pass a single id as a param for ids, the response is correct.

def get_stores_info
  uri = URI(BASE_URL)
  params = { ids: store_ids, offset: DEFAULT_OFFSET, limit: DEFAULT_LIMIT }
  uri.query = URI.encode_www_form(params)
  response = Net::HTTP.get_response(uri).body
  result = JSON.parse response
end
McNulty007
  • 23
  • 2
  • Do you want the ids to be just like that? `ids=2,24,13`? How are you handling that on the backend? Usually, `id[]=1&id[]=24&id[]=13` should be sent in query string for Rails to get in params as... `params[:id] = [2,24,13]`. – razvans Nov 13 '21 at 20:53
  • I don't have control over the backend, so I need to build the request like that: ds=2,24,13 – McNulty007 Nov 13 '21 at 22:05

1 Answers1

1

You can transform store_ids to string:

store_ids = [2,24,13]
params = { ids: store_ids.join(','), offset: 0, limit: 25 }

# these are to see that it works.
encoded = URI.encode_www_form(params) # => "ids=%5B2%2C+24%2C+13%5D&offset=0&limit=25"
CGI.unescape(encoded) # => ids=2,24,13&offset=0&limit=25

Here's a Replit.

razvans
  • 3,172
  • 7
  • 22
  • 30