Express.js req.acceptsCharsets() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Request

Definition

In Express.js, req.acceptsCharsets() checks the request’s Accept-Charset header and returns the first charset you offered that the client accepts (for example 'utf-8'). If none match, it returns false.

Use it when a client still sends charset preferences. For modern web apps, default to UTF-8 in responses — most browsers no longer send Accept-Charset at all.

Syntax

Call req.acceptsCharsets in your Express.js app. Start with this line.

Express
req.acceptsCharsets(charset)
req.acceptsCharsets(charset1, charset2, ...)
req.acceptsCharsets([charset1, charset2, ...])
CallWhat it does
req.acceptsCharsets(charset)Primary signature.
req.acceptsCharsets(charset1, charset2, ...)Related call form.
req.acceptsCharsets([charset1, charset2, ...])Related call form.

Install with npm install express. Create the app with const app = express() before you use this API in an Express.js tutorial project.

Library declarations
Express
const express = require("express");
const app = express();

req.acceptsCharsets(charset)
req.acceptsCharsets(charset1, charset2, ...)
req.acceptsCharsets([charset1, charset2, ...])

Parameters

ParameterDescription
charset(s)Required. One charset, several arguments, or an array — for example 'utf-8', 'iso-8859-1'.

Return Value

req.acceptsCharsets(...) returns a matching charset string, or false when none of the listed charsets are acceptable.

Express
const charset = req.acceptsCharsets('utf-8', 'iso-8859-1');
if (!charset) charset = 'utf-8'; // your default

Express.js Examples

Example 1 — Prefer utf-8 from Accept-Charset

Offer two encodings; Express returns the best accepted one from your list.

Express
const express = require('express');
const app = express();

app.get('/text', (req, res) => {
  // Accept-Charset: utf-8, iso-8859-1;q=0.5
  const charset = req.acceptsCharsets('utf-8', 'iso-8859-1');
  // => 'utf-8'

  res.type('text/plain; charset=' + charset).send('Hello');
});

app.listen(3000);

How It Works

  1. List encodings you support. Both arguments are candidates.
  2. Read Accept-Charset. The client ranks utf-8 above iso-8859-1.
  3. First accepted wins. Official docs show this case returning 'utf-8'.
  4. Advertise the choice. Put the charset on Content-Type so the client knows how to decode.

Example 2 — Unsupported Charset Returns false

When the client only accepts something you did not offer, the result is false.

Express
const express = require('express');
const app = express();

app.get('/legacy', (req, res) => {
  // Accept-Charset: us-ascii
  const charset = req.acceptsCharsets('utf-8', 'iso-8859-1');
  // => false

  if (!charset) {
    return res.status(406).send('Not Acceptable');
  }
  res.type('text/plain; charset=' + charset).send('ok');
});

app.listen(3000);

How It Works

  1. Offer only utf-8 / iso-8859-1. us-ascii is not in that list.
  2. Negotiation fails. Express returns false, matching the official example for req.acceptsCharsets('us-ascii') against a mismatched offer.
  3. Strict APIs send 406. That signals you cannot satisfy the charset preference.
  4. Many apps soft-fail instead. See the next example for a UTF-8 fallback.

Example 3 — Default to UTF-8 When Missing or Unmatched

Practical pattern for today’s clients that omit Accept-Charset.

Express
const express = require('express');
const app = express();

app.get('/note', (req, res) => {
  const charset = req.acceptsCharsets(['utf-8', 'iso-8859-1']) || 'utf-8';

  res
    .type('text/plain; charset=' + charset)
    .send('Saved note');
});

app.listen(3000);

How It Works

  1. Pass an array. Same idea as multiple arguments.
  2. Use || 'utf-8'. Covers false and keeps a safe default.
  3. Matches real browsers. They rarely send the header; UTF-8 is expected.
  4. Still honors real preferences. A client that asks for iso-8859-1 can get it when you support it.

Runtime Support

req.acceptsCharsets() is an Express.js request method. It runs on a Node.js server (Express 5 needs Node.js 18+). Browsers do not execute it — and most no longer send Accept-Charset.

✓ Server · Express 4 & 5

req.acceptsCharsets()

Works wherever Express runs: Node.js is the primary target. Bun and Deno can run many Express apps with compatibility layers. Not available in the browser.

Server Node.js runtime API
Node.js 18+ for Express 5 · 14+ typical for Express 4
Supported
Bun Express-compatible server apps
Supported
Deno Via npm compatibility / adapters
Supported
req.acceptsCharsets() Express on Node.js

Bottom line: Study and run these examples with Node.js and the express package. Copy a snippet into a local file and start the server with node.

Frequently Asked Questions

It checks the Accept-Charset request header and returns the first charset you offered that the client accepts. If none match, it returns false.
Most modern browsers omit Accept-Charset and expect UTF-8. The Express method is still useful for clients that send the header, or for learning negotiation.
Fall back to utf-8 for a better user experience, or send 406 if your API must refuse unsupported charsets.
Use multiple arguments or an array, for example req.acceptsCharsets('utf-8', 'iso-8859-1') or req.acceptsCharsets(['utf-8', 'iso-8859-1']).
No. It runs on your Node.js server. Browsers only send headers; most no longer send Accept-Charset at all.

Next: req.acceptsEncodings()

Negotiate content encodings such as gzip from Accept-Encoding.

req.acceptsEncodings() tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful