Skip to content
tezvyn:

Draft a JSON error response for an invalid authentication token

Source: rfc-editor.orgMediumHow cards are made

Draft a JSON error response for an invalid authentication token

Tests your ability to standardize API error contracts with RFC 7807. A strong answer returns 401 with type, title, detail, and instance, plus an actionable fix like re-authenticating. Red flag: 403, echoing the token, or exposing stack traces.

What's really being asked

This question tests whether you can design a standardized, secure, and developer-friendly HTTP error response. The interviewer cares about your familiarity with RFC 7807 Problem Details, your ability to choose semantically correct status codes, and your judgment about what information is safe to expose to API consumers. Senior engineers are expected to treat error responses as part of the API contract, not as afterthoughts.

The full answer

First, the status code must be 401 Unauthorized because the request lacks valid authentication credentials. Do not use 403 Forbidden, which signals that authentication succeeded but the client lacks authorization. Second, the Content-Type should be application/problem+json per RFC 7807. Third, the JSON body should include the standard members: type, a URI that identifies the problem class such as https://api.example.com/errors/invalid-token; title, a short human-readable summary like Invalid Authentication Token; status, repeating the 401 code; detail, a specific actionable message such as The provided token has expired or is malformed. Please authenticate again to obtain a valid token; and instance, a URI reference to the specific occurrence for correlation in logs. Fourth, mention extension members only if they add value, such as a retry after hint or a documentation link. Fifth, explicitly state what you omit: never echo the invalid token, never include stack traces, internal file paths, or database details.

The mistakes people make

Candidates often confuse 401 with 403. They sometimes return 400 Bad Request, which is too generic for an auth failure. Another red flag is inventing a bespoke error envelope without referencing RFC 7807, such as a simple message string or an error code integer that lacks documentation. Echoing the submitted token back to the client is a security anti-pattern because the token may be logged by proxies or browsers. Returning a generic 500 Internal Server Error for an auth failure breaks client trust and prevents retries. Finally, vague detail messages like Authentication failed without next steps frustrate developers.

What usually comes next

How would you handle rate limiting on your token endpoint? What if the token is valid but expired, would you change the response? How do you version your error type URIs? Would you support problem details in XML or HTML as well? How do you correlate the instance URI with distributed tracing IDs?

A concrete example

HTTP 401 Unauthorized with Content-Type application/problem+json and body containing type set to https://api.example.com/errors/invalid-auth-token, title set to Invalid Authentication Token, status set to 401, detail set to The bearer token is expired or malformed. Re-authenticate at POST /auth/token to obtain a new token, and instance set to /logs/errors/550e8400-e29b-41d4-a716-446655440000. This tells the developer precisely what went wrong, why, and how to fix it, while keeping sensitive server state out of the response.

Interview question

Which response best conforms to RFC 7807 and security best practices for an invalid authentication token?

  • a.Return 401 Unauthorized with Content-Type application/json, use a bespoke body with only error_message set to "Auth failed", and omit type and instance
  • b.Return 403 Forbidden with Content-Type application/problem+json, echo the submitted token in the detail field for debugging, and set status to 403
  • c.Return 401 Unauthorized with Content-Type application/problem+json, include type URI, title, status 401, actionable re-authentication detail, instance URI, and omit the token valueCorrect
  • d.Return 400 Bad Request with Content-Type application/problem+json, include a server stack trace in the detail field, and omit the type URI
Why?

RFC 7807 requires 401 for invalid credentials, the application/problem+json content type, and standard members such as type and instance, while security best practices forbid echoing the token or exposing stack traces. Option A correctly uses 401 but fails to adopt the RFC 7807 format, whereas A and B use wrong status codes and leak sensitive information.

Just read this? Test yourself on what you have been reading.

Read the original → rfc-editor.org

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on api design — each one lists the topics its interview covers.

See open roles