ThreeMammals/Ocelot · error · InvalidOperationException

More data ( bytes) received than the specified…

Error message

More data ({contentLength} bytes) received than the specified Content-Length of {announcedContentLength} bytes.

What it means

Thrown from StreamHttpContent.CopyAsync (invoked via SerializeToStreamAsync when Ocelot serializes the proxied request body): while streaming the incoming request stream into the outgoing request content, the accumulated byte count contentLength exceeds announcedContentLength, the Content-Length header the client declared. Normally the server enforces this, but a proxy component that modified or duplicated the body without updating Content-Length desynchronizes the two values. CopyAsync detects the mismatch mid-copy, throws InvalidOperationException, and abandons the forward — the pool buffer is returned and the request fails rather than sending a body that violates the promised length to the downstream service. The input at fault is the request body stream/its Content-Length header pair, corrupted by an upstream modifier (e.g. a DelegatingHandler or middleware that rewrote the body).

Solutions

  1. Find and fix the component (custom middleware, DelegatingHandler, request mapper) that modified the proxied body without updating the Content-Length header.
  2. Remove or recompute Content-Length after body modification, or switch the outgoing request to chunked transfer (no Content-Length) when the length is unknown.
  3. Log the original and rewritten bodies with their lengths to identify where the extra bytes are introduced.
  4. Guard any custom IStreamContent/body rewriting code so it can only shrink or must resize Content-Length accordingly.
  5. Retry-safe option: catch the InvalidOperationException at the request-serialization boundary and return 400 Bad Request to the client, since the client's own declared length was violated.
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/Request/Mapper/StreamHttpContent.cs:81 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of ThreeMammals/Ocelot@d1f22d9304 (2026-09-12). Data as JSON: /api/errors/db74f584892b1e3e. Report an issue: GitHub.

Appendix: source

Thrown at src/Request/Mapper/StreamHttpContent.cs:81

                else
                {
                    // Take care not to return the same buffer to the pool twice in case zeroByteReadTask throws
                    var bufferToReturn = buffer;
                    buffer = null;
                    ArrayPool<byte>.Shared.Return(bufferToReturn);

                    await zeroByteReadTask;

                    buffer = ArrayPool<byte>.Shared.Rent(minBufferSize);
                }

                var read = await input.ReadAsync(buffer.AsMemory(), cancellation);
                contentLength += read;

                // Normally this is enforced by the server, but it could get out of sync if something in the proxy modified the body.
                if (announcedContentLength != UnknownLength && contentLength > announcedContentLength)
                {
                    throw new InvalidOperationException($"More data ({contentLength} bytes) received than the specified Content-Length of {announcedContentLength} bytes.");
                }

                // End of the source stream.
                if (read == 0)
                {
                    if (announcedContentLength == UnknownLength || contentLength == announcedContentLength)
                    {
                        return;
                    }
                    else
                    {
                        throw new InvalidOperationException($"Sent {contentLength} request content bytes, but Content-Length promised {announcedContentLength}.");
                    }
                }

                await output.WriteAsync(buffer.AsMemory(0, read), cancellation);
                if (autoFlush)
                {

View on GitHub (pinned to d1f22d9304)