0

I encounter the following case: I have an API that allows me to retrieve a unique resource:

GET myapi/resource_id

If this resource does not exist, I return a 404 HTTP status code.

This API also allows to retrieve several resources via the same request:

GET myapi/resource_id1,resource_id2

Which HTTP status code should I send back if one of the two resources does not exist?

  1. 200 with an explanation in the JSON in an error key

  2. 206 which allows to be more explicit with an explanation in the JSON in an error key

  3. 400 / 404 code but this is not fine because the API still returns one of the two information

  4. another solution?

Thank you for your help.

JCDUS
  • 1
  • 2
  • Are you returning 404 code or it is returning automatically. – Md Riadul Islam Feb 13 '19 at 15:57
  • While it may sound like a great idea to batch mutliple resources with a single request, you loose the property of making use of a cache for separate resources and eventually also get an unusable ETag value for that "batch-resource". HTTP is clearly not designed for such a strategy. The actual expensive thing to do is open a connection for each request, but as now plenty of clients support `Connection: keep-alive` sending mutliple requests via the same connection isn't an issue. The HTTP protocol guarantees that messages are received in the correct order here – Roman Vottner Feb 13 '19 at 15:57
  • I'm returning `404`code for a unique retrieve but I don't know which code I have to use for a multiple retrieve – JCDUS Feb 13 '19 at 16:50

1 Answers1

0

I think there are 2 options here.

  • Send back 200 and don't even mention the missing resource.
  • Send back 404 with an empty body.

The 206 is for range requests, the 400 is for malformed requests, so none of those apply here.

inf3rno
  • 24,976
  • 11
  • 115
  • 197