{"record":{"id":"e2f04bfaae9ed5b7","repo":"denoland/deno","slug":"err-http-content-length-mismatch","errorCode":"ERR_HTTP_CONTENT_LENGTH_MISMATCH","errorMessage":"Response body's content-length of ${this[kBytesWritten]} byte(s) does not match the content-length of ${this._contentLength} byte(s) set in header","messagePattern":"Response body's content-length of (.+?) byte\\(s\\) does not match the content-length of (.+?) byte\\(s\\) set in header","errorType":"exception","errorClass":"NodeError","httpStatus":null,"severity":"error","filePath":"ext/node/polyfills/_http_outgoing.ts","lineNumber":614,"sourceCode":"        return this;\n      } else if (!this._header) {\n        if (this.socket) {\n          this.socket.cork();\n        }\n\n        this._contentLength = 0;\n        this._implicitHeader();\n      }\n\n      if (typeof callback === \"function\") {\n        this.once(\"finish\", callback);\n      }\n\n      if (\n        _checkStrictContentLength(this) &&\n        this[kBytesWritten] !== this._contentLength\n      ) {\n        throw new ERR_HTTP_CONTENT_LENGTH_MISMATCH(\n          this[kBytesWritten],\n          this._contentLength,\n        );\n      }\n\n      const finish = FunctionPrototypeBind(onFinish, undefined, this);\n\n      if (this._hasBody && this.chunkedEncoding) {\n        this._send(\"0\\r\\n\" + this._trailer + \"\\r\\n\", \"latin1\", finish);\n      } else if (!this._headerSent || this.writableLength || chunk) {\n        this._send(\"\", \"latin1\", finish);\n      } else {\n        (globalThis as any).process.nextTick(finish);\n      }\n\n      if (this.socket) {\n        // Fully uncork connection on end().\n        this.socket._writableState.corked = 1;","sourceCodeStart":596,"sourceCodeEnd":632,"githubUrl":"https://github.com/denoland/deno/blob/89f33cbef296a2b287f323d42de54c871fa69c77/ext/node/polyfills/_http_outgoing.ts#L596-L632","documentation":"On end(), OutgoingMessage enforces strict content-length: when _checkStrictContentLength(msg) is true (msg.strictContentLength enabled AND a content-length is set AND the message has a body AND it is not chunked and content-length was not removed), it compares kBytesWritten (bytes actually written) with _contentLength (the declared header value). Any difference throws ERR_HTTP_CONTENT_LENGTH_MISMATCH instead of sending a lying header.","triggerScenarios":"setHeader('content-length', Buffer.byteLength(bodyA)) then end(bodyB) where the bodies differ; string vs byte miscounts (content-length computed on .length of a multibyte string); streams where the declared length does not match actual chunk sizes under strictContentLength servers.","commonSituations":"Manually setting content-length for keep-alive reuse but the body is transformed (gzip, template substitution) after counting; using str.length instead of Buffer.byteLength(str, encoding) for UTF-8; mutating the body between the length computation and end().","solutions":["Compute the length from the exact bytes sent: Buffer.byteLength(body, encoding)","Apply all transformations (compression, templating) before measuring and setting content-length","For unknown-size streams, remove the content-length header and use chunked transfer-encoding instead"],"exampleFix":"// before\nres.setHeader('content-length', body.length); // 'héllo'.length = 5, bytes = 6\nres.end(body);\n\n// after\nres.setHeader('content-length', Buffer.byteLength(body));\nres.end(body);","handlingStrategy":"validation","validationCode":"const body = Buffer.from(payload, 'utf8');\nres.setHeader('content-length', body.length);\nres.end(body); // byte count always matches under strictContentLength","typeGuard":null,"tryCatchPattern":"try { res.end(body); } catch (e) { if (e.code === 'ERR_HTTP_CONTENT_LENGTH_MISMATCH') { /* recompute length from actual bytes and reissue response */ } else throw e; }","preventionTips":["Always measure with Buffer.byteLength / Buffer.byteLength(s, enc), never string.length","Apply transforms (gzip, templating) before setting content-length","For unknown sizes, delete content-length and let chunked encoding apply"],"tags":["http","content-length","encoding","validation","node-compat"],"backgroundTag":null,"analyzedSha":"89f33cbef296a2b287f323d42de54c871fa69c77","analyzedAt":"2026-08-16T07:54:21.310Z","schemaVersion":2},"datasetVersion":"2026-08-16T08:17:34.114Z"}