It depends on the semantics you want to give. E there are controversies as to that. Some people categorically state one thing, others are more thoughtful. So don’t consider this an answer that says what you should do, I’m putting some possibilities for you to decide.
Some weights
Think about the non-web API, if you had methods in the same executable, what would you return? It would be a array empty? Or would be an exception? The 404 in this case is like the exception.
There are those who like to make exceptions everywhere and then the 404 would make sense. I think you should inform that something wrong happened otherwise. For me exceptions should only occur if something exceptional has been found. If something normal happens it should not generate an exception. And having zero items is a normal thing to do, isn’t it? Maybe not what one wanted, but it’s a normal response?
There’s technology that likes the exceptions to everything.
I don’t like the 404 because it’s the same answer you would have if you put a URL and it doesn’t exist at all, so you’re using the same code for two different things. I would agree with another 400, but none seems appropriate to me. If it is another at least indicates something else. If you find another that looks good then you should use it.
Why not use the 204 No Content
? It seems more appropriate, at least it doesn’t mix things up. And who knows would have the array empty. So you have a normal response, which worked, but a clear indicative that the data should not be used. I’m not saying it’s a good use like that, but it’s better than 404, for me. At least it says the request was successful, but has no valid data.
Some people like up to 200 and do not indicate in the code that there is no data, this should be checked in the resulting message internally. This is appropriate if you want to leave the HTTP code only to indicate what happened with the transport and its second hypothesis. Give up the REST.
Reading the RFC gives an indication that this is a technology that likes the exceptions. There on page 8 is an excerpt that it treats the data as the resource you are wanting to access. So is the 404 not the most technically correct medium for this case? See:
A network data Object or service that can be identified by a URI, as defined in Section 3.2. Resources may be available in Multiple representations (e.g. Multiple Languages, data formats, size, and resolutions) or Vary in other Ways.
Completion
I don’t like the use of HTTP code to give semantics of the system, but the people who love REST decided to adopt so, I’m not going to tell you what is good for you.
So you can’t decide for yourself, they both work if you do it right and they don’t work if you do it wrong. The most important thing is to be consistent.
One thing that can help you is to ask users what they use with other Apis, it may indicate consistency, but be wary, not always the user is right, often he is the person who least understands the subject.
As there is no time to give a more dignified answer, it goes right here: returns 404, an empty array is a return possibility, the id does not exist, no.
– Gabriel Oliveira
I think at the end this question will end up "based on opinions". This is a very controversial topic. Some people think that returning 404 is a security breach, since there is an indication whether the user exists or not. There are those who think they should even return 404, because no user will be found. And there will be those who think should return 400 (bad request) or even some 5xx.
– tvdias
@tvdias 400 (bad request) would never be used for this, unless the person does not know what is doing, it has nothing to do with "server problem", it has to do with problem coming from client side: The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to Something that is Perceived to be a client error (e.g., malformed request syntax, invalid request message Framing, or deceptive request routing)
– Guilherme Nascimento
@Guilhermenascimento, as I said, there are those who argue that it is a user’s mistake, since one does a GET to
users/:id
would return a 404.– tvdias
@tvdias One thing is the ID does not exist, another is to write an ID in "invalid format", if staying relativizing will start to create "myths"
– Guilherme Nascimento
@Guilhermenascimento therefore even the answers are based on opinions. Just as you have yours, other people have others. And even the answer is all like this
Não gosto do 404 porque é
,porque não usar o 204 No Content?
, etc....– tvdias
Recommended reading on the subject’s "opinion" or not: Subjective good, subjective bad
– Bacco
@tvdias I’m not talking about the question and the answer, I just talked about something that you said it wasn’t and that if someone ever told you to do/use (even if you disagreed with the person) it’s because you didn’t know what you were doing. On the merits of the question, I nay I got into it in no time.
– Guilherme Nascimento
id in an invalid format I am returning 400 even, my doubt is only if the id does not exist, since it is a request of the documents and the id is of the user, but at the same time are the documents of this user.
– Fernando Ribeiro da Silva
@Guilhermenascimento, please read my comment again. And as I said, just as you have the your opinion, other people may disagree. I have the mine and because I thought that I would be "one more of many".
– tvdias
@tvdias has no opinion, you said that someone said something, but whoever said it there probably did not know what you were doing, even if you believed that person’s conversation or not. As I said, it is in this form of "achism" (not yours, but that supposed person you mentioned) that the "myths" (mistakes) are born, are people doing wrong things and just because "works" begin to affirm that it is right or that it is a way of doing, being in fact it is all a mistake.
– Guilherme Nascimento