Roee Gavirel
Roee Gavirel

Reputation: 19453

REST Get for multi values with multi keys

I have a RESTful server that implement get request for an entity while the entity have multi keys. For example, getting contact information for a specific company in a specific country (Nike, Germany):

GET: http://hostname/rest/accounts/{company}/{country}

I want to add a functionality it to allow the client to query multiple company/countries pairs in one call. Since I have millions of records in the DB I don't ever want to return all the data. Also, the client may need ~1000 records, so I don't want him to make ~1000 calls.

I thought of adding the pairs of company/country in the body of the request, but the answer here HTTP GET with request body suggested it's a bad practice.

I can't use the query string params because I have to much information and most servers have a limit on the size of the URL.

What's a good REST practice for such a case?

Upvotes: 0

Views: 512

Answers (1)

mahemoff
mahemoff

Reputation: 46479

There is an official proposal for a multi-request protocol - here (background). Possibly because it relies on HTTP/2 to deliver the efficiency gain, it doesn't seem to have much momentum at present.

For a few resources, you could specify them all in the URL. The problem is the de-facto URL limit of ~2000 characters. While it applies primarily to IE, I think a good API should work well in browsers, and it probably applies elsewhere because of IE's restriction.

For that reason, I think you have two choices.

Realistically you will need to use a POST request where the body contains a list of requested paths, e.g. as a JSON-encoded array. It's generally bad practice as the reference suggests, but it's generally considered reasonable in this situation. The practical downside is it's not cacheable.

Upvotes: 1

Related Questions