Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Best practice for passing range or arrays to API

Not applicable

Hi Community!

I have the following API design question.

What is the best approach for passing ranges and arrays to a GET request?

I have these use cases:

  • Search people by age of 10, 20 or 30 years old (array)
  • Search people between 10 and 45 years old (range)

What I can think of is:

/People?age={10,20,30} <-- accolades = array

/People?age=(10,45) <-- brackets = range

or

/People?age=(10,20,30) <-- comma = array

/People?age=(10-45) <-- dash = range

Are these correct ways and is there any best practice?

Kind regards,

Jan Willem

Solved Solved
2 8 29.3K
1 ACCEPTED SOLUTION

I hesitate to claim this is the "best" approach, but here's where I would start with this one. First, I see this as two different cases, and because of that I would approach this two different ways.

In the first, where we are searching for people of specific ages (array case), I would just use multiple age parameters, e.g.

GET /people?age=10&age=20&age=30

In the second, where we are searching for people within a range of ages, I would use two different query parameters, e.g.

GET /people?minage=10&maxage=45

I like this because I think it's harder to make a mistake. I don't need to remember a specific convention, etc.

View solution in original post

8 REPLIES 8