Search code examples
restgetquery-string

REST GET query parameter advice


I might be making this more complicated than it should be, but I'm wondering what would be considered "good practice" for a GET endpoint to search for one or more entities of a resource. I'd also like to make it easy to get them "all". For context, I'm wanting people to be able to look up the details tied a productId that can be at multiple (300-400 and growing) locations. It's a not an overly taxing operation for one-few locations, but if a user were to want all relevant locations back, it could be an expensive api call.

Which of these would be best?

Specify which locations using a list and assume "all locations" if it's not provided

/product/{productId}/search?locations=[1,2,3]

Create a allLocations query param to specify a user wants to look at all locations

/product/{productId}/search?allLocations=True

Solution

    • Firstly, you can use comma separated values intead of array.
    ?location=1,2,3
    
    // Then in backend, you can explode/split it which will be much easier.
    
    • For best practises, get result from all locations when location query parameter is empty or unavailable.
    • For making API calls less expensive, you can restrict the number of selected locations at a time.