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
:
[]
db.json
. I've updated the question with the json-server command. This is using vanilla json-server so the server can be tested withGET http://localhost:5000/posts?authorId=does_not_exist
json-server
as a minimal code mock API & was trying to see ifjson-server
could be configured to return 404 when query parameters have no match