Context
Attempting to make a HTTP GET
request for posts filtered by query parameter authorId=x
where x
may be a number that may not correspond to any post's authorId
.
Problem
json-server
unexpectedly returns HTTP 200
instead of HTTP 404
response when there are no posts with a matching authorId
(i.e., an empty array is returned), how can this be changed to return 404
? Similarly, what would be the best API practice here, would it be to return an empty array with HTTP 400
as json-server
already does or would returning the empty array with HTTP 404
be more clear for users?
I've looked at jsonServer.rewriter
& express
middleware (e.g., json-server
documentation shows it can be configured with middleware such as server.use(middlewares)
) but wanted to ask what the best approach would be here (useful resources/links would be appreciated here), e.g., with middleware, an option is sending 404
for an empty array but does json-server
have an builtin ways to handle this or is there a better approach?
All constructive feedback is welcome, thanks.
Code
db.json
:
{
"posts": [
{
"authorId": 0,
"content": "Foo bar"
},
],
}
Shell:
json-server --watch db.json
REST:
// Response status is expected HTTP 200.
GET http://localhost:5000/posts?authorId=0
This returns the user with HTTP 200
as expected:
[
{
"authorId": 0,
"content": "Foo bar",
}
]
// Response status is unexpected HTTP 200 but 404 was expected since response body contains an empty array. This is the problem.
GET http://localhost:5000/posts?authorId=does_not_exist
This returns an empty array with HTTP 200
, which is possibly unexpected (uncertain on what the best practice is regarding filtered collections where there's no match, but what would be the best practice here & how can the status be changed to HTTP 400
:
[]
CodePudding user response:
As @kindall pointed out, probably ill-advised to return 404, but perhaps this is the existing behavior of the API you're mocking. You can return custom output - from docs: https://github.com/typicode/json-server#custom-output-example
// In this example we simulate a server side error response
router.render = (req, res) => {
res.status(500).jsonp({
error: "error message here"
})
}
For your example, it would probably be something like (untested pseudo-code):
// In this example we return 404 for no content
router.render = (req, res) => {
if (res.locals.data.posts.length < 1) {
res.status(404).jsonp({
error: "no posts"
});
} else {
res;
}
}