Web API request content is empty

I have a DelegatingHandler implementation to log the content of the request / response:

public class RequestAndResponseLoggerDelegatingHandler : DelegatingHandler
{
    public IDataAccess Data { get; set; }

    protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
    {
        var started = DateTime.UtcNow;
        var response = await base.SendAsync(request, cancellationToken);
        await Log(started, request, response);
        return response;
    }

    private async Task Log(DateTime start, HttpRequestMessage request, HttpResponseMessage response)
    {
        var finished = DateTime.UtcNow;
        var requestContent = await request.Content.ReadAsStringAsync();
        var responseContent = await response.Content.ReadAsStringAsync();
        var info = new ApiLogEntry(start, finished, requestContent, responseContent, request, response);
        Data.Log(info);
    }
}

      

but for some reason requestContent

comes up empty. request.Content.Length

confirms there is content, it just isn't being retrieved.

Any ideas?

+3


source to share


1 answer


The stream of the request body is read and bound to parameters, and as a result of the binding, the stream is placed at the end. This is why it becomes empty. If you're looking for a start before request.Content.ReadAsStringAsync()

, it should work. Instead, you can simply read the body of the request before the binding happens, something like this.



public class RequestAndResponseLoggerDelegatingHandler : DelegatingHandler
{
    public IDataAccess Data { get; set; }

    protected override async Task<HttpResponseMessage> SendAsync(
                            HttpRequestMessage request, 
                                 CancellationToken cancellationToken)
    {
        var started = DateTime.UtcNow;
        var requestContent = await request.Content.ReadAsStringAsync();

        var response = await base.SendAsync(request, cancellationToken);

        var responseContent = await response.Content.ReadAsStringAsync();
        await Log(started, request, response, requestContent, responseContent);
        return response;
    }

    private async Task Log(DateTime start, HttpRequestMessage request, 
                             HttpResponseMessage response, string requestContent, 
                                string responseContent)
    {
        var finished = DateTime.UtcNow;           
        var info = new ApiLogEntry(start, finished, requestContent, responseContent, 
                                        request, response);
        Data.Log(info);
    }
}

      

+7


source







All Articles