0

I've searched a lot about this and have found different answers, also, my case is a little different.

The context: I have a document A with a possible sender S

Server X (so not a browser) requests the sender from document A on Server Y, but the sender is absent.

What should server Y return:

  • 200: with a null object (not really OK and dangerous for nullpointers on Server X)
  • 204: a correct status I think, but this is mainly used when the endpoint does not return data in general (e.g. post, update, delete), which can be confusing
  • 404: this should definitely the answer for .../sender/{sender_id}. But in this case we ask the sender of a document, and no sender is a correct answer...

So, what would be the best practice, or is there another approach which is better fitting for this.

Thanks in advance!

Thomas Stubbe
  • 1,945
  • 5
  • 26
  • 40
  • I would use 404, but it also depends how the consumers of your API use your API and how other resources are handled. Consistency is important. – Francois Sep 21 '17 at 14:25

3 Answers3

1

I would suggest 404 Not Found matches best what you describe as "sender is absent"

Just in case here you can see a list of status codes: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

nbari
  • 25,603
  • 10
  • 76
  • 131
1

Broad rule: don't try to make status codes specific the details of your api or your domain model. Those are messages to generic components (like browsers, caches, proxies) that don't need to know anything about the specifics of your domain model and your integration protocol.

404: this should definitely the answer for .../sender/{sender_id}. But in this case we ask the sender of a document, and no sender is a correct answer...

The 4xx class of response codes indicate an error in the client request. In other words, the client asked the question wrong. 404 specifically implies that the client addressed the request to the wrong integration resource.

So it's not what you are looking for.

204: a correct status I think, but this is mainly used when the endpoint does not return data in general (e.g. post, update, delete), which can be confusing

204 has a very specific meaning - is says that the representation provided in the response is zero bytes long. "You asked me to send you the contents of this file, and I'm successfully doing so, but by the way the file is empty."

So if the representation of an absent sender is zero bytes long, aces! But if the representation is instead an empty json object

{}

Then 204 is off the table.

200 is probably your best bet.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
0

There are two cases here.

  1. If you are requesting a collection eg: /users and there are no users available on the server then your service should return 200 OK with an empty list.

  2. If you are requesting a resource by id eg: /users/id and if the user is not available then you should return 404 NOT Found as the user id that you are searching is not available on the server.

Based on your situation(assuming you are requesting for a collection of resources), I probably recommend returning 200 OK with the empty list. If you does not have control over the server then you may need to have a null check on the client side.

  • I know the above two cases, and we implemented that way everywhere in our code, but here it isn't an collection... The correct implementation would be an Optional... but I suppose that is not the way to implement a rest api :p – Thomas Stubbe Sep 22 '17 at 10:16