11

I've the following endpoint:

a/{id}/b

and want to create a b with sending POST request to it. If a with given {id} is not found should I response with 404 NOT_FOUND or maybe with 409 CONFLICT?

It is to handle plain a/{id}, the trick is that here a subresource is used.

1

2 Answers 2

16

404 NOT FOUND seems to be an appropriate response, because the resource with this ID does not exist. It's very clear to understand and you are expecting the same response if a/{id} is called.

409 CONFLICT does not seem to be the better choice, because in your example you will return a 409 when the parent resource was not found :).

But remember that the most important thing is be consistent in your API.

7
  • I agree. If you were to try to write to a folder that didn't exist, is it a conflict or a missing folder error? It seems more intuitive this way to me.
    – Neil
    Commented Jan 24, 2018 at 12:31
  • A "folder" you mean a path that does not exists?
    – Dherik
    Commented Jan 24, 2018 at 13:05
  • I mean a folder, like on the file system.
    – Neil
    Commented Jan 24, 2018 at 14:16
  • Can you detail the scenario? Because it depends. If the folder was expected for the server and (for whatever reason) are not there I think this is a server error (5xx) not a client error (4xx). If this folder value was passed by the client to the resource (as the id), is 404. But if the folder was passed in the body this could be anything else (412, 422... something to represent "validation failed: folder does not exists"). It's a good question to ask and discuss.
    – Dherik
    Commented Jan 24, 2018 at 14:27
  • If you ask the server for file with path /nonexistent/help.html, and the folder /nonexistent doesn't exist, there is only one clear response to this. 404 file not found! There could even exist a /home/help.html, and the response would not differ. This is a REST application clearly, but I see no reason why the logic would change. The parent must exist first.
    – Neil
    Commented Jan 24, 2018 at 14:42
7

In addition to @Dherik's answer.

URIs are identifiers, so we have to keep in mind that (/a/{id}/b is an identifier). The URI is meaningless for the WWW, and so is for the HTTP client.

404 is the right answer. In essence, the server is answering

I didn't find any resource with such id. Resource not found 1

Whether the missing resource is parent or child doesn't matter.

We, developers, see hierarchies and paths but HTTP clients don't. In other words, we read and see infirmation in these identifiers but HTTP clients don't. These treat URIs as meaningless strings.

In case of doubts, don't ask what code makes sense for you (human). Do ask, what code makes sense for the HTTP client. How do I want the HTTP client to behave?

Why? Because status codes make the client to behave in different ways. For example, 302. This code usually makes web browsers to redirect to a specific location (URI) informed in the response headers.

This might not be your case, but it's important to know about this. Ultimately, HTTP status codes are addressed to HTTP clients. Not to our applications. Not to persons.


1: 409 is rarely implemented as navigation error. It usually involves the execution of remote operations (delete, update, new, etc). But the URI should exist. Otherwise, 404 will prevail

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.