A data request from another data must respond with 404 or an empty array?

Asked

Viewed 65 times

0

I am working with an API which has the following route users/:id/documents.

This route should return a list of documents from a specific user.

For example:

the route users/:id/documents return all user documents with id 1.

if it has no document, the route will return an empty array with 200 status, but if the request is made with a id nonexistent user should return 404 or an empty array in the same way?

  • 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.

  • 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 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)

  • @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 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"

  • @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....

  • 2

    Recommended reading on the subject’s "opinion" or not: Subjective good, subjective bad

  • @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.

  • 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.

  • @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 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.

Show 6 more comments

1 answer

2


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.

  • o povo que ama REST resolveu adotar assim Yeah, I think REST has helped a lot of things, but it has its deficiencies and then they force you to do something that doesn’t make any sense pq no REST é assim.

  • @tvdias REST is done as each likes and has no standard format at all, and probably AR did not say that REST was the problem, the problem is the "people who love" and began to invent "rules" and "myths". No one forces you to do anything (unless you’re your project manager or boss), you only do so if you don’t know what you’re doing or you’re following "cake recipe".

  • Thank you very much, it helped me a lot to clarify my doubt.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.