12

I want to get Http Request body in .net core , I used this code:

using (var reader
    = new StreamReader(req.Body, Encoding.UTF8))
{
    bodyStr = reader.ReadToEnd();
}
req.Body.Position = 0

But I got this error:

System.ObjectDisposedException: Cannot access a disposed object. Object name: 'FileBufferingReadStream'.

An error occurs after the using statement of the line

How to get HttpRequest Body in .net core? and how to fix this error?

rayan periyera
  • 123
  • 1
  • 1
  • 6
  • See Tseng's comment below my answer (https://stackoverflow.com/questions/52849296/reading-the-response-body-stream-in-filter/52850301#52850301) . [Api](https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.http.internal.bufferinghelper.enablerewind?view=aspnetcore-2.1) – itminus Oct 23 '18 at 10:29
  • I still get the same error – rayan periyera Oct 23 '18 at 10:31
  • Possible duplicate of [Reading the Response.Body stream in Filter](https://stackoverflow.com/questions/52849296/reading-the-response-body-stream-in-filter) – Linda Lawton - DaImTo Oct 23 '18 at 10:33
  • Give us some more context, where are you using this code for example ? Show some more surrounding code. – andynormancx Oct 23 '18 at 10:34
  • the code snippet won't throw the exception. If you can provide more details of your code it would help us to understand your problem. – Ravikumar Oct 23 '18 at 10:45

4 Answers4

10

use this extension method to get httpRequest Body:

   public static string GetRawBodyString(this HttpContext httpContext, Encoding encoding)
    {
        var body = "";
        if (httpContext.Request.ContentLength == null || !(httpContext.Request.ContentLength > 0) ||
            !httpContext.Request.Body.CanSeek) return body;
        httpContext.Request.EnableRewind();
        httpContext.Request.Body.Seek(0, SeekOrigin.Begin);
        using (var reader = new StreamReader(httpContext.Request.Body, encoding, true, 1024, true))
        {
            body = reader.ReadToEnd();
        }
        httpContext.Request.Body.Position = 0;
        return body;
    }

The important thing is that HttpRequest.Body is a Stream type And when the StreamReader is disposed, HttpRequest.Body is also disposed.

I had this problem until I found the below link in GitHub: Refer to the below link and the GetBody method https://github.com/devdigital/IdentityServer4TestServer/blob/3eaf72f9e1f7086b5cfacb5ecc8b1854ad3c496c/Source/IdentityServer4TestServer/Token/TokenCreationMiddleware.cs

VahidNaderi
  • 2,448
  • 1
  • 23
  • 35
amirhamini
  • 526
  • 5
  • 14
2

Simple workaround:

using (var content = new StreamContent(Request.Body))
{
     var contentString = await content.ReadAsStringAsync();
}
jcmontx
  • 419
  • 5
  • 13
1

Please review answer by Stephen Wilkinson

Use the following for .NET Core 3.1

Startup.cs

app.Use((context, next) =>
{
    context.Request.EnableBuffering(); // calls EnableRewind() `https://github.com/dotnet/aspnetcore/blob/4ef204e13b88c0734e0e94a1cc4c0ef05f40849e/src/Http/Http/src/Extensions/HttpRequestRewindExtensions.cs#L23`
    return next();
});

You should then be able to rewind as per other answers:

httpContext.Request.Body.Seek(0, SeekOrigin.Begin);
Lance
  • 592
  • 2
  • 7
  • 13
0

The accepted answer did not work for me, but I was reading the body twice.

    public static string ReadRequestBody(this HttpRequest request, Encoding encoding)
    {
        var body = "";
        request.EnableRewind();

        if (request.ContentLength == null ||
            !(request.ContentLength > 0) ||
            !request.Body.CanSeek)
        {
            return body;
        }

        request.Body.Seek(0, SeekOrigin.Begin);

        using (var reader = new StreamReader(request.Body, encoding, true, 1024, true))
        {
            body = reader.ReadToEnd();
        }

        //Reset the stream so data is not lost
        request.Body.Position = 0;

        return body;
    }
eddyP23
  • 6,420
  • 7
  • 49
  • 87