Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
json-server returns HTTP 200 instead of 404 for query parameter filtered GET request, what is the best practice here & how can 404 be returned?

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:

[]
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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.jsonp(res.locals.data);
  }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!