Unable to read HttpContext Body

Selvakumar Ramachandran 1 Reputation point
2021-12-17T03:44:28.003+00:00

Hi,
I am trying to read request information from request body but always getting as empty string. Please help me if anyone have answer for my query. I am using .Net Core 5 and C#.

Flow
SearchController > SearchMethod(SearchContent info) - from controller will call the repository method
SearchRepository > SearchRepositoryMethod(SearchContent info) - Inside this method calling an external api. I implemented a delegate handler and configured in HttpClient. Inside delegate handler I am trying to access the request body. But request body is always returning empty.

public abstract class BaseHandler : DelegatingHandler
{
public BaseAuditHandler(IHttpContextAccessor accessor)
{
_accessor = accessor;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
HttpContext httpcontext = _accessor.HttpContext;
var request = httpcontext.Request;
request.EnableBuffering();
request.Body.Position = 0;
var requestBody = await new System.IO.StreamReader(request.Body).ReadToEndAsync();
}
}

Thank you
Selvakumar R

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,020 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,963 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jaliya Udagedara 2,716 Reputation points MVP
    2021-12-17T05:23:39.307+00:00

    Can you try something like this,

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        HttpContext httpcontext = _accessor.HttpContext;
        HttpRequest request = httpcontext.Request;
        string requestBodyString;
        try
        {
            request.EnableBuffering();
            var buffer = new byte[Convert.ToInt32(request.ContentLength)];
            await request.Body.ReadAsync(buffer, 0, buffer.Length);
            requestBodyString = Encoding.UTF8.GetString(buffer);
        }
        finally
        {
            request.Body.Position = 0;
        }
    
        // TODO: use requestBodyString
    }
    

  2. Azaan Azeem 1 Reputation point
    2021-12-20T08:29:31.74+00:00

    This is a problem that can occur when you are trying to access the body of an HTTP context. The body of an HTTP context contains information about the request that was made, and can be useful for debugging purposes. However, if you are unable to read the body of the HTTP context, then you will not be able to access this information.

    There are a few possible reasons why you might be unable to read the body of an HTTP context. One possibility is that your application is not configured to allow access to the body of the HTTP context. Another possibility is that the body of the HTTP context has been encrypted, and therefore cannot be accessed.

    0 comments No comments