Gerg
Gerg

Reputation: 172

What API response should I send when a consumer asks for a collection of resources related to a resource that doesn't exist?

Let's say I have a resource

/providers/123

When a consumer sends a GET for that, it receives 200 OK and a record for the provider identified by 123, or a 404 Not Found if there is no provider with id 123. Fine.

Now let's say I have another resource

/providers/123/publications

which represents a collection of publications authored by provider 123. When a consumer sends a GET for that:

But what if the provider doesn't exist? 404? 200 with empty set? 200 with empty set and a warning? Something else?

Upvotes: 0

Views: 64

Answers (2)

Shreyas Chavan
Shreyas Chavan

Reputation: 1099

Depends on your end user (the entity making the API calls)

If its a human being then 200 with an error saying that the provider doesnt exist seems the best. This case is applicable mostly with single calls to your API

If the end user is a robot parsing data from your resultset then 200 with an empty set will be a good choice.This case is applicable when API calls are made in bulk.

Example: This Geonames API gives country code from latitude and longitude, but if I have invalid latitude and longitude, it will throw an error message

http://api.geonames.org/countryCode?lat=181&lng=181&username=demo

Upvotes: -1

Ed Broome
Ed Broome

Reputation: 75

404 for sure. The URL refers to a resource that does not exist.

Don't give any more information than is required.

Upvotes: 2

Related Questions