5

I will explain my doubt by throwing an example.

Suppose i am designing an endpoint POST for student resource and as part of the POST endpoint i want a teacher uuid to be passed (apart from other details) which is a mandatory field. So that i can associate the student to the teacher. Now if the teacher resource with the given uuid is not present in my DB then what should i throw:

400 (Bad request)
404 (Not found).

I think 400 would be the correct thing.

If i had a request like below then i would have thrown 404 like

GET /xyy/teachers/{uuid of a teacher}.

Correct me if i am wrong. Thanks.

Trying
  • 14,004
  • 9
  • 70
  • 110
  • AFAIR you can **not** return more than one status code, so I would say that you will have to choose. My view is that if UUID is **not** supplied the status code 400 is better, if UUID is invalid status code 404 is good – Germann Arlington Mar 29 '15 at 10:23
  • Personally, I agree with 400. There needs to be a generic "the client did something wrong code". If it doesn't fit something in the 4xx family, then 400 should fit the bill. I don't 404 either, as it is not the resource being requested. – Paul Samsotha Mar 29 '15 at 11:47
  • But he's requesting a teacher so why ia that no resource? – Ria Mar 29 '15 at 11:49
  • @Ria I think you misread the question. I think the OP is requesting the `student` resource, whose representation has a property that relates to the teacher. The teacher might be looked up on the server end, from the teacher id to see if it exists. From what I understand about the question, that is the case – Paul Samsotha Mar 29 '15 at 12:16
  • Generally when POSTing to create a new resource, there should be no place for a 404. It's already obvious the resource doesn't exist. – Paul Samsotha Mar 29 '15 at 12:26
  • But then the semantic question arises, why should the student be assigned a teacher (when first being created) in the first place on a create request. Maybe on a PUT update request might seem more appropriate to update the student after it is already in the system. With it required in a create request, you are basically saying the "at the time of enrollment, the student must have a teacher assigned to them", which may be the case, but more often then not, isn't – Paul Samsotha Mar 29 '15 at 12:41
  • Does this answer your question? [What's the most appropriate HTTP status code for an "item not found" error page](https://stackoverflow.com/questions/5604816/whats-the-most-appropriate-http-status-code-for-an-item-not-found-error-page) – Bill Horvath Sep 21 '21 at 21:39

2 Answers2

0

I'd prefer 404 because the resource you requested is not available. If you have a look at w3.org spec they say for 400

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.

As your syntax is not malformed, it sounds like a classic 404 to me.

Ria
  • 1,900
  • 4
  • 14
  • 21
0

This is actually a good question. As others have pointed out, neither 400 (Bad Request) nor 404 (Not Found) seem like a perfect fit for this kind of situation.

  • HTTP 400 (Bad Request) tends to be used to signal bad syntax or a malformed request, something perceived as a client error. In your situation, the request is completely valid and is compliant with what the server expects.
  • HTTP 404 (Not Found) is usually returned when a target resource does not exist or when the server doesn't want to disclose that one exists. In your scenario, one could argue that the "target resource" (a student) is not the cause of the issue and, hence, a 404 could be semantically confusing.

As an alternative, let me suggest a 422 (Unprocessable Entity) response, part of the WebDAV extensions. It meant to cover cases in which the server...

[...] understands the content type of the request entity, and the syntax of the request entity is correct, but it was unable to process the contained instructions.

This reads like a really good fit for your problem.

Nicolás Fantone
  • 2,055
  • 1
  • 18
  • 24