Medium15 minKoa.js Fundamentals
UpdatedAug 5, 2026
Edit

Koa Content Negotiation

Question Variations

  • "How do `Accept` and `Content-Type` differ in Koa?"
  • "When should an API return 406?"
  • "How can Koa return JSON or text based on client preference?"

Why This Is Asked

Koa exposes HTTP negotiation helpers directly on the context. This question tests whether you distinguish client request formats from accepted response formats and implement predictable HTTP behavior.

Key Concepts

  • Content-Type describes a representation sent in a request or response.
  • Accept declares response representations the client can handle.
  • ctx.accepts() selects from explicitly supported representations.
  • 415 and 406 represent different media-type failures.

Question Variations

  • “How do Accept and Content-Type differ in Koa?”
  • “When should an API return 406?”
  • “How can Koa return JSON or text based on client preference?”

Answers by Technology

+ Add Variant
Koa.jsImprove this answer ✏️

Expected Answer (Koa 3.2.1 / Node.js 18+)

Content-Type identifies the representation sent in a request or response, while Accept lists representations the client can receive. Use ctx.accepts() to choose from a small, explicit list of formats the route supports. Return 415 when an incoming body uses an unsupported media type and 406 when no acceptable response format is available. For cacheable negotiated responses, vary by Accept.

Why It Matters

Media-type correctness prevents parser confusion and makes API behavior predictable for different clients.

Code Example

import Koa, { Context } from 'koa';

const app = new Koa();
app.use((ctx: Context) => {
  const format = ctx.accepts('json', 'text'); ctx.vary('Accept');
  if (format === 'json') ctx.body = { ok: true };
  else if (format === 'text') { ctx.type = 'text'; ctx.body = 'ok'; }
  else ctx.status = 406;
});
app.listen(3000);

Common Mistakes

  • Using Accept to validate a request body: The request body is identified by Content-Type.
  • Ignoring cache variation: A cache can return the wrong representation.

Follow-up Questions

  • When is 415 appropriate? (Answer: The request body’s media type is unsupported.)
  • What does 406 mean? (Answer: No supported response representation matches the client’s Accept header.)