So I have this:
app.Use(async (context, next) =>
{
context.Request.EnableBuffering();
await next();
});
And this:
private async Task<JObject> GetRequestBodyAsync(HttpRequest request)
{
JObject objRequestBody = new JObject();
try
{
// IMPORTANT: Leave the body open so the next middleware can read it.
using (StreamReader reader = new StreamReader(
request.Body,
Encoding.UTF8,
detectEncodingFromByteOrderMarks: false,
leaveOpen: true))
{
string strRequestBody = await reader.ReadToEndAsync();
objRequestBody = JsonConvert.DeserializeObject<JObject>(strRequestBody);
}
}
finally
{
// IMPORTANT: Reset the request body stream position so the next middleware can read it
request.Body.Position = 0;
}
return objRequestBody;
}
But although request.Body.Length > 0 (as expected), this line:
string strRequestBody = await reader.ReadToEndAsync();
always returns an empty string. Any ideas?
The next action that follows retrieves [FromBody] OK.