Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get my Ruby on Rails API controller to receive an array of values for a query parameter?

I’m moving an API endpoint from an Express service to a Ruby on Rails service. In Express, when multiple values are provided for the same query parameter, for example http://localhost:3002/api/resource?include=foo&include=bar, Express treats them as an array. The query parameters to the previous endpoint would be { include: ['foo', 'bar'] }.

The default behavior I’m seeing in Ruby on Rails is to override the previous query param when multiple query params are used with the same key. For example, the previous endpoint’s query params would be translated as { include: 'bar' }. Is it possible to change this behavior to allow for an array of values instead of overriding the previous value?

Since this is a preexisting API endpoint, I cannot change the URL to fix this problem. I know that http://localhost:3002/api/resource?include[]=foo&include[]=bar would work, but this would be a breaking change to the API.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

CGI::parse request.query_string

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading