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 27.4K
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

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.

I like this approach.

Not applicable

Hi Carlos, thank you for your answer. I like your solution!

But, what I thought was the beauty of my approach, is that you can combine variables, for instance:

GET /people?age="10-30,40"

This way, you get people between 10 and 30, and people of the age of 40.

Would this be a restfull approach?

Sure, I think that would be restful. It's still cacheable, doesn't misuse http methods, etc. I think you could still make that happen with the other method, but it wouldn't be as compact. Depending on how much filtering you need to do on people you might want to consider something more powerful, for example passing in a query via a "q" parameter or something... GET /people?q=(age > 10 and age < 30) or age = 40 (url encoded naturally).. that's going to get more complex on the implementation side but if you have a ton of variety you want to support it might be hard to find a nice set of query params that enable that. All depends on the use case, I guess. It's all about finding the right level of "simple" I think. 😉

Thnx! I will discuss further with the team!

Hi @Dino : In shopping cart experience what is the best approach to pass single or multiple skus in query parameter?

In case of multiple skus:

URL?skus=["sku1","sku2","sku3"] OR

URL?skus="sku1","sku2","sku3" OR

URL?skus=[sku1,sku2,sku3] OR

URL?skus=sku1,sku2,sku3

In case of single skus:

URL?skus=["sku1"] OR

URL?skus="sku1" OR

URL?skus=[sku1] OR

URL?skus=sku1

Appreciate your quick help on this!

Thanks,

Radhika

A. you've asked a new question in an answer. That's not what answers are for. If you want to ask a question, ask a question.

7046-ask-a-question.png

B. Your question seems to be exactly the same as the original question here. What is different? Please don't answer that here. Provide this information when you ask the new question.

C. What did you not like about the original, accepted answer from three years ago? It's possible that this answer is inappropriate for you. If so, explain why? Again, not here. In the new question.

Sure. Thanks @Dino