Api Design easy
A client sends invalid JSON to your API. What should the response include?
- A Return a 200 OK status containing a JSON body with { "error": true, "message": "Bad JSON" }.
- B Return a 400 Bad Request status with a structured error body describing the validation failure.
- C Return a 500 Internal Server Error to indicate the server failed to process the input.
- D Close the TCP connection immediately to save server resources.
Answer: B. Return a 400 Bad Request status with a structured error body describing the validation failure.
When a client sends invalid JSON, the server cannot parse the request. Returning a **400 Bad Request** is the standard way to indicate a client-side error. Providing a structured error body (e.g., with an error code and message) is crucial because it allows the client to programmatically parse the error and show a useful message to the user.
One system design question a day, with spaced repetition so the ones you miss come back.
Get the appRelated questions
- You're designing a REST API for a blog. Which endpoint best represents 'get all comments for post 123'?
- A client wants to update only the 'status' field of an order, leaving other fields unchanged. Which HTTP method is most appropriate?
- A client calls POST /users with valid data, and the user is created. What status code should the server return?
- A client retries a failed 'POST /payments' request due to network timeout. The server receives the request twice. Without idempotency handling, what can happen?
- An API returns 10,000 products. Returning all in one response is impractical. Which pagination approach is simple and stateless?
- Your API serves both mobile apps and third-party integrations. Which authentication method is most appropriate?