0

What's the idiomatic way to signal the caller about an error in API, in particular, "Not found" error? Suppose, I have an url /api/vi1/check/123. What should I return in case

  1. There is no such a record with id = 123? 404 error (Http status 404)?

    status: 404 (http); data: { }

  2. Or should I return OK status (Http status 200) and json with the internal status

    status: 200 (http); data: { status: 404, msg: "Not found"}?

  3. And (related to 1) what should I return if there is no such an url? Also Http 404 status?

Yes, I've seen the best practice videos and read the articles about REST APIs but this question wasn't answered clearly in those.

1

3 Answers 3

2

REST is about using HTTP's semantics. It also applies to error codes. You should use the 404 status code, and eventually a more descriptive error message in the body.

Sign up to request clarification or add additional context in comments.

2 Comments

And (related to 1) what should I return if there is no such an url? Also Http 404 status? This doesn't seem logical.
Following REST principles, a not found error and an URL not existing is the same thing. An URL is the location of the resource.
1

Yes you should return a simple 404, no more no less. (Maybe a message to go with that...) That is indicating that the resource you are looking for is not there which is good practice in REST.

Comments

-1

It is also good practise not to provide any body here (like an internal status code, or message). If you really need a message in such a case (404 or 500 on an error) you can do that with an additional HTTP header field (e.g. X-Message: Database not available). But a 404 with empty response body is really sufficient here.

3 Comments

and would an user know if the url doesn't exist or the record doesn't exist without the message?
Like @toasted_flakes said, this is the same thing when thinking in resources. As your services returns JSON encoded data it will be consumed by some client and not accessed by the browser directly (AJAX, Android, JavaFX, etc.). Your client calling the URL should know the base URL (e.g. /tasks/) and notify the user about a not found error (e.g. /tasks/123 got an 404).
About the messages: Don't get me wrong it is perfectly fine to add a message, but it should not be returned as the response body. When thinking in resouces you are accessing e.g. a resource task (/tasks/123). So you are expecting the task with ID 123 to be returned in the format you specified in the Accept header field (application/json, application/xml etc). HTTP 200 OK {"name": "My task", "dueDate": 123456789} When your application is not able to return it e.g. due to a database error, it should return the status by following RFC2616.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.