Contents
Should I return 204?
204 No Content is not terribly useful as a response code for a browser (although according to the HTTP spec browsers do need to understand it as a ‘don’t change the view’ response code). 204 No Content is however, very useful for ajax web services which may want to indicate success without having to return something.
Does rest have to return JSON?
REST APIs should accept JSON for request payload and also send responses to JSON. JSON is the standard for transferring data. Almost every networked technology can use it: JavaScript has built-in methods to encode and decode JSON either through the Fetch API or another HTTP client.
Should you put a 404 return?
In the second case an error should be returned, but not a 404 — possibly a 412 Precondition Failed or maybe just a 400 Bad Request. Thoughts? IMHO from the client perspective you are starting at the root ( / ) and walking down the path. If at any point some path part is not found, you should throw 404 eagerly.
Should a put return 404?
Is RESTful always JSON?
For some, REST means a server that exchanges JSON documents with a client over HTTP. Not only is that not a complete definition, but it’s also not always true. The REST specification doesn’t require HTTP or JSON. (The spec doesn’t mention JSON or XML at all.)
What should I expect from a RESTful API?
The first thing to consider, since you are building a RESTful API, is to return an appropriate response code. And the more appropriate response code to communicate that the request went through normally, but the requested resource is not available at the moment is the venerable 404.
When to return an empty array in API?
This approach also answers your question. You should return an empty array when the query does not match: On the other hand, if you try to get a specific movie resource with GET api/movie/34 and that movie does not exist, then return 404 with a suitable (json encoded) error message in the body
What’s the proper rest response for an empty table?
Let’s say you want to get list of users by calling GET to api/users, but currently the table was truncated so there are no users. What is the proper response for this scenario: 404 or 204? I’d say, neither. Why not 404 (Not Found) ? The 404 status code should be reserved for situations, in which a resource is not found.
When to return a representation of an empty collection?
In case of api/users, you usually expect to receive a representation of your collection of users. Sending a response body one time and not sending it the other time is inconsistent and potentially misleading. For reasons mentioned above (consistency), I would return a representation of an empty collection. Let’s assume you’re using XML.