sveltejs/kit · error · Error
Invalid content-type value "${type}". ${error_suffix}
Error message
Invalid content-type value "${type}". ${error_suffix} What it means
The content-type value's media type (part before ';') must match a type/subtype pattern. Values like 'text' (missing subtype), 'invalid' bare tokens, or whitespace-mangled types throw with the parsed type and original value shown.
Source
Thrown at packages/kit/src/runtime/server/validate-headers.js:45
const parts = value.split(',').map((part) => part.trim());
if (parts.some((part) => !part)) {
throw new Error(`\`cache-control\` header contains empty directives. ${error_suffix}`);
}
const directives = parts.map((part) => part.split('=')[0].toLowerCase());
const invalid = directives.find((directive) => !VALID_CACHE_CONTROL_DIRECTIVES.has(directive));
if (invalid) {
throw new Error(
`Invalid cache-control directive "${invalid}". Did you mean one of: ${[...VALID_CACHE_CONTROL_DIRECTIVES].join(', ')}? ${error_suffix}`
);
}
},
'content-type': (value) => {
const type = value.split(';')[0].trim();
const error_suffix = `(While parsing "${value}".)`;
if (!CONTENT_TYPE_PATTERN.test(type)) {
throw new Error(`Invalid content-type value "${type}". ${error_suffix}`);
}
}
};
/**
* @param {Record<string, string>} headers
*/
export function validateHeaders(headers) {
for (const [key, value] of Object.entries(headers)) {
const validator = HEADER_VALIDATORS[key.toLowerCase()];
try {
validator?.(value);
} catch (error) {
if (error instanceof Error) {
console.warn(`[SvelteKit] ${error.message}`);
}
}
}View on GitHub (pinned to 03f1687fe6)
Solutions
- Use the full media type: e.g. 'application/json', 'text/html; charset=utf-8'
- Prefer Response.json() or the json() helper so the content-type is set correctly for you
- Trim whitespace and ensure a '/' separates type and subtype
Example fix
// before
event.setHeaders({ 'content-type': 'json' });
// after
event.setHeaders({ 'content-type': 'application/json' }); Defensive patterns
Strategy: validation
Validate before calling
const CT = /^[*\w.+/-]+\/[\w.+-]+$/;
function isValidContentType(v) { return CT.test(v.split(';')[0].trim()); }
// if (!isValidContentType('application/json')) throw ... Try / catch
try {
event.setHeaders({ 'content-type': type });
} catch (e) {
if (e.message.startsWith('Invalid content-type')) console.error('Bad media type:', type);
} Prevention
- Use full MIME types like application/json, never bare suffixes
- Prefer Response.json()/json() helpers over manual content-type
- Always include type/subtype separated by '/'
When it happens
Trigger: setHeaders({ 'content-type': 'json' }) instead of 'application/json'; returning a Response whose headers include a malformed content-type like 'application/;' ; empty content-type string.
Common situations: Hand-rolled endpoint responses with abbreviated types; string concatenation that drops the subtype; copying only the MIME suffix ('xml', 'html') instead of the full type.
Related errors
- `cache-control` header contains empty directives. ${error_su
- Invalid cache-control directive "${invalid}". Did you mean o
- Invalid value for environment variable ${env_prefix + name}:
- The ${protocol_header} header specified ${protocol} which is
- Could not determine host from the ${host_header ? `${host_he
AI-assisted analysis of sveltejs/kit@03f1687fe6 (2026-09-02).
Data as JSON: /api/errors/07413057c231d1e3.
Report an issue: GitHub.