EasyπŸ‘€ 0-2 yearsπŸ‘€ 3-5 years 2 min read

HTTP Status Codes with JSON β€” Interview Questions

Asked inAmazonMicrosoftDeloitteAccenture
#json#http status codes#error handling#rest#api design
Report issue

⚑ Short Answer

The status code carries the outcome (2xx success, 4xx client error, 5xx server error) and the JSON body carries the detail. Success bodies hold the resource; error bodies hold a consistent error shape (code, message, maybe field errors). Anti-pattern: returning 200 with {"error":...} β€” clients then can't rely on the status code.

β˜•Coffee Chat Question

Concept Made Simple

β€œHow do HTTP status codes work together with JSON responses?”

🧠Mind Map Answer

Remember It Faster

2xx + JSON→resource / result body
400/422 + JSON→validation error details
401/403 + JSON→auth error message
5xx + JSON→generic error (no internals)

Keep one consistent error envelope across the whole API so clients parse errors the same way everywhere.

⌨️Hands-on Keyboard

Learn by Doing

http
HTTP/1.1 422 Unprocessable Entity
Content-Type: application/json

{
  "error": "validation_failed",
  "message": "Email is invalid",
  "fields": { "email": "must be a valid email" }
}

πŸ”₯What If?

Think Beyond the Expected

Why is returning HTTP 200 with an error body considered a bad practice?

It breaks the contract clients, proxies, caches, and monitoring rely on. Tooling treats 2xx as success, so a 200 'error' won't trigger retries, alerts, or error handling β€” the failure hides. Use the correct 4xx/5xx status AND a JSON error body: the code for machines, the body for humans/details.

πŸ˜‚Real World

Mature APIs define one error envelope (code, message, requestId) and always pair it with the right status. Front-ends branch on res.ok/status; observability tools alert on 5xx rates β€” all of which relies on honest status codes.

🎯Interviewer's Expectation

Keywords they're listening for:

βœ“ status = outcome, body = detailβœ“ 2xx/4xx/5xx familiesβœ“ consistent error envelopeβœ“ don't return 200 for errorsβœ“ hide internals in 5xx

⚠️Common Mistakes

  • βœ—Returning 200 for failures
  • βœ—Inconsistent error shapes across endpoints
  • βœ—Leaking stack traces in 5xx bodies

βœ…Best Practices

  • βœ“Match status code to the real outcome
  • βœ“Standardize one error envelope
  • βœ“Return generic 5xx bodies (no internals)

πŸ”Follow-up Questions

  • 1What's the difference between 400 and 422?
  • 2How do you design a consistent error schema?
  • 3Which errors should be retried by clients?

🧩Related Technologies

HTTPRESTerror handlingobservability

Continue Learning with AI

Take this question deeper with your favourite AI assistant. Pick a depth, copy the prompt, or open it directly β€” AI is your learning companion, not a shortcut.

Plain-language foundations

I'm preparing for a software engineering interview and want to understand this from scratch, as a beginner.

Topic: REST APIs (JSON)
Interview question: "How do HTTP status codes work together with JSON responses?"

Please:
1. Explain the core idea in simple, plain language, using an everyday analogy.
2. Define any technical terms you use.
3. Walk through one small, concrete example.
4. Finish with a single sentence I can easily remember.

Keep the tone friendly and assume I'm new to this topic.

Was this answer helpful?

Support our platform by exploring our recommended products.

As an Amazon affiliate, purchases through these links may earn us a small commission β€” at no extra cost to you. It helps keep Full Stack Interview Guru free.

Related Questions