1

I have a collection of IDs of RESTful resources (all the same type of resource), the number of which can be indefinitely large. I want to make a REST call to get the names of these resources. Something like this:

Send:

['005fc983-fe41-43b5-8555-d9a2310719cd', '4c6e6898-e519-4bac-b03e-e8873d3fa3f0',...]

Receive:

['Resource A', 'Resource B',...]

What is the best way to retrieve the names of these resources RESTfully?

Here are the ideas I have had and the problems I see with each approach:

  • The naive approach is to iterate through all IDs in my collection and do a 'GET /resource/:id' for each ID. This would be prohibitively slow and resource intensive because of the large number of HTTP calls I would have to make.
  • The next approach I thought of is to pass the IDs as parameters to a single GET call. The problem here is that most servers have a limit on the URL length, which would be quickly exceeded.
  • Next, I thought that putting the IDs in the body of a GET would work, but according to Roy Fielding, data in the GET body should not affect the results of a REST call: HTTP GET with request body
  • I could use a POST request and put the data on the POST body, but POST is intended for creating and modifying resources, which is not what I'm doing. Maybe I should ignore the intent of the verb and use it anyway?
  • I could split the request into multiple GET requests to avoid exceeding the max URL length. The problem here is that I have to combine the results after all calls have returned, which is potentially slow.
  • I could create a collection resource within my main resource by posting my list of IDs to 'POST /resource/collection', then use a 'GET /resource/collection/:id' call to retrieve the results. This actually works, but then I have to do a 'DELETE /resource/collection/:id' to clean up. It takes multiple calls, requires cleanup, and seems a bit clunky overall, so it's okay, but not ideal.

Is there a better way to do this?

Community
  • 1
  • 1
David Hansen
  • 2,148
  • 2
  • 16
  • 17

1 Answers1

1

Your last approach is RESTful and the one I recommend. I'd do this:

Step 1:

Request:

POST /resource/collection
Content-Tpye: application/json

{
  "ids": [
    "005fc983-fe41-43b5-8555-d9a2310719cd",
    "4c6e6898-e519-4bac-b03e-e8873d3fa3f0"
  ]
}

Response:

201 Created
Location: /resource/collection/89AB8902-FDF1-11E4-ADDF-CD4FB664A5DC

Step 2:

Request:

GET /resource/collection/89AB8902-FDF1-11E4-ADDF-CD4FB664A5DC

Response:

200 OK
Content-Type: application/json

{
  "resources": [ ... ]
}

but then I have to do a 'DELETE /resource/collection/:id' to clean up.

Not, that is not necessary. The server could implement a job that removes all collections that are older than a specific timestamp. It is not the client who has to do this.

If later a client access the collection again, the server would respond with

410 Gone