New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
HttpRequest.BodyReader.TryRead returns false even though request has a body #55615
Comments
This is a misunderstanding. TryRead returns false if data isn't immediately available. TryRead or ReadAsync may still return data in the future. The request may have a large body, but it isn't available yet. I would also suggest trying this without request body buffering, that construct can skew your results since it involves pipe<->stream adapters, you don't have direct access to the server buffers. |
Thanks for replying @Tratcher . To clear all doubts, I tried your suggestion, I removed |
You can put that all back, it was just removed to simplify the scenario. The real fix is to replace TryRead with ReadAsync. |
Ok. but say I need to use public static T? ReadBody<T>(this HttpRequest httpRequest)
{
var bodyReader = httpRequest.BodyReader;
ReadResult readResult = default;
do
{
if (!bodyReader.TryRead(out readResult))
continue; // continue instead of breaking here ??
bodyReader.AdvanceTo(readResult.Buffer.Start, readResult.Buffer.End);
} while (!readResult.IsCanceled && !readResult.IsCompleted);
var buffer = readResult.Buffer;
if (buffer.IsEmpty)
return default;
var reader = new Utf8JsonReader(buffer);
return JsonSerializer.Deserialize<T>(ref reader, JsonExtensions.JsonOptions);
} |
If TryRead returns false then you need to call ReadAsync next, not stop reading entirely. |
in that case, I will stick to just using |
Is there an existing issue for this?
Describe the bug
HttpRequest.BodyReader.TryRead
returnsfalse
(andReadResult
isn't populated) even though the request has a body and thereadResult
isn't completed or cancelled.When using
HttpRequest.BodyReader.ReadAsync
, theReadResult
response is populated successfully.Here is a Demo that reproduces the issue.
Expected Behavior
HttpRequest.BodyReader.TryRead
should not returnfalse
when a request body exists and thereadResult
isn't cancelled or completed.Steps To Reproduce
Clone this Repo, build and run.
Fire a
POST
request tohttps://localhost:7244/bodyreaderbugdemo/working
with the request body belowNotice that the response is a
200 Ok
POST
request tohttps://localhost:7244/bodyreaderbugdemo/buggy
with the same request body as above.Notice that we get a
400 Bad Request
Key Notes
Both endpoints employ filters that shortcircuits if the read request body is
null
.The first endpoint uses a filter
ExtractRequestBodyWorking
which usesHttpRequest.BodyReader.ReadAsync
to read request body. This works as expected.However, the second endpoint uses another filter
ExtractRequestBodyBuggy
which usesHttpRequest.BodyReader.TryRead
to read request body. in this case,HttpRequest.BodyReader.TryRead
returnsfalse
even though a request body exists.Exceptions (if any)
No response
.NET Version
8.0.200
Anything else?
No response
The text was updated successfully, but these errors were encountered: