In RESTful APIs, designing a request that carries complex, structured data like an array of objects can be a challenge. Since URLs have a length limit and complex structures are hard to represent in a query string, there isn't a one-size-fits-all solution, but rather some common practices and approximations.
For your case, the goal is to make a GET request that essentially carries the payload [{ "id": 10, "from": 100, "to": 1000}, { "id": 20, "from": 200, "to": 2000 }] as a query.
One common approach is to serialize the structured data into a compact string format, like JSON, and then either send it directly as a query parameter (taking care to properly URL-encode it) or use other strategies to avoid direct inclusion of potentially large or complex data in the URL.
Direct Serialization Approach
You could directly serialize your array of objects into a JSON string and then URL-encode this string before appending it to the query string of your URL. However, due to the complexity and length of this string, it's not always the most practical or efficient approach, especially for GET requests. This approach might look like this (before URL encoding):
GET /search?filters=[{"id":10,"from":100,"to":1000},{"id":20,"from":200,"to":2000}]
After URL-encoding, it might look more complex and potentially exceed URL length limitations for some servers or clients.
Parameter Repetition Approach
Another common way is to use parameter repetition where each object's property is sent as a separate parameter and repeated for each item. This doesn't perfectly preserve the array structure but can be reconstrued on the server-side:
GET /search?id=10&from=100&to=1000&id=20&from=200&to=2000
On the server side, you'd parse these parameters, noting that each sequence of "id", "from", "to" corresponds to a single object. This method requires some logic on both client and server sides to correctly pair these parameters together again into objects.
Keyed Object Approach
A variation of the above, more structured, could involve parameter naming that includes both a key for each object and the field names:
GET /search?filter[0][id]=10&filter[0][from]=100&filter[0][to]=1000&filter[1][id]=20&filter[1][from]=200&filter[1][to]=2000
This way, on the server side, you could parse each filter[n] as an object based on its keys, reconstructing the original array structure more intuitively.
Considerations
-
Complexity vs. Practicality: If your real-world scenario frequently requires sending complex structured data in GET requests, it might be worth reconsidering the API's design. Perhaps use POST requests for complex queries, despite the traditional RESTful use of GET for data retrieval.
-
Performance and Limits: GET URLs have length limits (which can vary by browser and server), making the direct serialization approach impractical for large datasets.
-
Security: Including sensitive or detailed data structures in URLs (which might be logged or cached) could have security implications.
Ultimately, the best approach depends on your specific needs, the capabilities of the client and server technology you're using, and the complexity of the data you need to handle.