{"record":{"id":"02bb3e98acc20bd6","repo":"nodejs/node","slug":"und-err-req-content-length-mismatch-02bb3e","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-h2.js","lineNumber":1660,"sourceCode":"          socket[kReset] = true\n        }\n\n        client[kResume]()\n      }\n    }\n  )\n\n  util.addListener(pipe, 'data', onPipeData)\n\n  function onPipeData (chunk) {\n    request.onBodySent(chunk)\n  }\n}\n\nasync function writeBlob (abort, h2stream, body, client, request, socket, contentLength, 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    h2stream.cork()\n    h2stream.write(buffer)\n    h2stream.uncork()\n    h2stream.end()\n\n    request.onBodySent(buffer)\n    request.onRequestSent()\n\n    if (!expectsPayload) {\n      socket[kReset] = true\n    }\n\n    client[kResume]()\n  } catch (err) {","sourceCodeStart":1642,"sourceCodeEnd":1678,"githubUrl":"https://github.com/nodejs/node/blob/1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e/deps/undici/src/lib/dispatcher/client-h2.js#L1642-L1678","documentation":"Thrown by writeBlob() in client-h2.js (HTTP/2) as RequestContentLengthMismatchError. Same logic as the H1 path: when the body is a Blob, undici compares the declared content-length to body.size before writing; on mismatch it refuses to send a request whose frame data would contradict the header.","triggerScenarios":"An HTTP/2 request (Client constructed with allowH2 and target speaking h2) with body: Blob and a content-length header that does not equal blob.size.","commonSituations":"Same as the H1 case: hard-coded/cached content-length, a proxy rewriting the header, or a Blob rebuilt after the length was measured; only matters when the connection negotiates HTTP/2.","solutions":["Omit the content-length header for Blob bodies — undici derives it from body.size.","If you set it manually, guarantee it equals blob.size byte-for-byte.","Recompute the length from the same buffer referenced by the Blob."],"exampleFix":"// before\nawait client.request({ method: 'POST', path: '/', body: blob, headers: { 'content-length': '100' } })\n// after\nawait client.request({ method: 'POST', path: '/', body: blob })","handlingStrategy":"validation","validationCode":"function h2BlobRequest(client, path, blob, headers = {}) {\n  delete headers['content-length']\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":["Let undici derive content-length from blob.size on HTTP/2 as well as HTTP/1.1.","Recompute length from the same buffer referenced by the Blob."],"tags":["undici","client-h2","http2","content-length","blob","body"],"backgroundTag":null,"analyzedSha":"1b2de5e052fc0fb95fd7fb6846dcec4ade598e9e","analyzedAt":"2026-08-13T00:53:24.642Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}