rayan periyera
rayan periyera

Reputation: 133

How to get HttpRequest body in .net core?

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?

Upvotes: 13

Views: 14567

Answers (4)

amirhamini
amirhamini

Reputation: 536

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

Upvotes: 11

jcmontx
jcmontx

Reputation: 439

Simple workaround:

using (var content = new StreamContent(Request.Body))
{
     var contentString = await content.ReadAsStringAsync();
}

Upvotes: 2

Lance
Lance

Reputation: 632

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);

Upvotes: 1

eddyP23
eddyP23

Reputation: 7025

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;
    }

Upvotes: 0

Related Questions