Menu

My Logging Middleware Ate the Request Body
📰

My Logging Middleware Ate the Request Body

DEV Community: webdev·Sukhpinder Singh·about 18 hours ago
#qz6irB8U
#devtowebdev#devto#dev#body#request
Reading 0:00
15s threshold

I added request-body logging to an API one afternoon, the kind where you dump the raw payload so you can see exactly what a client sent. Harmless, right? The next morning a teammate pinged me because one of the POST endpoints had started rejecting perfectly good orders with validation errors. The payload looked fine in the logs. The handler swore the body was empty. Both were telling the truth.

The request body is a forward-only stream. Once something reads it to the end, the next reader gets nothing. My logging middleware read it to log it, and by the time model binding looked, there was nothing left to bind.

I wanted to see the byte counts instead of just nodding along to that explanation, so I built the smallest thing that reproduces it. One middleware that reads the body, one handler that reads it again, and a client that POSTs a real JSON payload over loopback. This is .NET 10, Release build, in a small Linux container. Nothing fancy.

Here's the shape of it.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.