RESTful API, what if the query string isn't lo

2019-05-18 19:35发布

问题:

We have a resource collection for products: /products.

We want to filter this collection to only return the members which have one of a list of specific class id's. For example:

GET /products?classes=100,101,102

This should return a collection of product members which have any of the classes listed.

The issue we have, is that we're working with thousands of products and classes, so the class list of id's could be thousands long - too long for a query string.

I'm keen to stick to RESTful principles whenever we can, so I like the fact that the resource /products?classes=100,101,102 when called with GET returns a filtered products collection.

Obviously, we could include the id's list in the body in JSON format, but that would mean that the call GET /products won't return a representation of the state of the resource (the resource being the URL), because the body is being used to provide filter options.

What's the best way to request a collection which is filtered, but the filter options are too long to use the query string..?

回答1:

Interesting comment from @C. Smith who suggests making a POST call using a X-HTTP-Method-Override header set to GET and passing the id's in the body. This would work.

After thinking about it we're probably going to limit the number of class id's allowed in the query string, and suggest making multiple calls, breaking up the id's list into say, groups of 200. Submitting more than 200 would return an error.

GET /products?classes=1004,2342,8753... (limited to 200 id's)

GET /products?classes=2326,3343,6981... (limited to 200 id's)

Then the results can easily be stitched together after.

This method would let you use 5,000 id's for example, by doing 25 calls, which while not ideal, is ok for our use case.



标签: rest