{"record":{"id":"2c69f700e783e7d0","repo":"nodejs/node","slug":"und-err-req-content-length-mismatch","errorCode":"UND_ERR_REQ_CONTENT_LENGTH_MISMATCH","errorMessage":"Request body length does not match content-length header","messagePattern":"Request body length does not match content-length header","errorType":"exception","errorClass":"RequestContentLengthMismatchError","httpStatus":null,"severity":"error","filePath":"deps/undici/src/lib/dispatcher/client-h1.js","lineNumber":1554,"sourceCode":"    abort(err)\n  }\n}\n\n/**\n * @param {AbortCallback} abort\n * @param {Blob} body\n * @param {import('./client.js')} client\n * @param {import('../core/request.js')} request\n * @param {import('net').Socket} socket\n * @param {number} contentLength\n * @param {string} header\n * @param {boolean} expectsPayload\n * @returns {Promise<void>}\n */\nasync function writeBlob (abort, body, client, request, socket, contentLength, header, expectsPayload) {\n  try {\n    if (contentLength != null && contentLength !== body.size) {\n      throw new RequestContentLengthMismatchError()\n    }\n\n    const buffer = Buffer.from(await body.arrayBuffer())\n\n    socket.cork()\n    socket.write(`${header}content-length: ${contentLength}\\r\\n\\r\\n`, 'latin1')\n    socket.write(buffer)\n    socket.uncork()\n\n    request.onBodySent(buffer)\n    request.onRequestSent()\n\n    if (!expectsPayload && request.reset !== false) {\n      socket[kReset] = true\n    }\n\n    client[kResume]()\n  } catch (err) {","sourceCodeStart":1536,"sourceCodeEnd":1572,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/dispatcher/client-h1.js#L1536-L1572","documentation":"Thrown by writeBlob() in client-h1.js (HTTP/1.1) as RequestContentLengthMismatchError (code UND_ERR_REQ_CONTENT_LENGTH_MISMATCH). When the request body is a Blob, undici compares the declared content-length to body.size before writing; if they differ, the wire bytes would contradict the header, so it refuses to send a malformed request.","triggerScenarios":"request({ method: 'POST', body: new Blob([...]), headers: { 'content-length': 'N' } }) where N !== blob.size; passing a content-length header that you computed against a different (mutated) blob.","commonSituations":"Hard-coding or caching a content-length value while the Blob is rebuilt asynchronously; a proxy/middleware that rewrites content-length; mutating the Blob's underlying buffer after size was measured.","solutions":["Do NOT set content-length manually for Blob/Buffer bodies — let undici compute it from body.size.","If you must set it, ensure the value equals body.size exactly (same byte length).","Recompute the length from the exact buffer you are about to send."],"exampleFix":"// before\nawait client.request({ method: 'POST', path: '/', body: blob, headers: { 'content-length': '100' } }) // != blob.size\n// after — let undici derive content-length\nawait client.request({ method: 'POST', path: '/', body: blob })","handlingStrategy":"validation","validationCode":"function blobRequest(client, path, blob, headers = {}) {\n  delete headers['content-length'] // let undici derive from blob.size\n  if (blob.size !== undefined && headers['content-length'] !== undefined) {\n    if (Number(headers['content-length']) !== blob.size) throw new RangeError('content-length != blob.size')\n  }\n  return client.request({ method: 'POST', path, body: blob, headers })\n}","typeGuard":"const lengthMatchesBlob = (headers, blob) => headers['content-length'] == null || Number(headers['content-length']) === blob.size","tryCatchPattern":"try { await client.request(req) } catch (e) { if (e.code === 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH') { delete req.headers['content-length']; /* retry */ } else throw e }","preventionTips":["Never set content-length manually for Blob/Buffer bodies — undici computes it.","Measure length from the exact buffer you send."],"tags":["undici","client-h1","content-length","blob","body","validation"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}