22

I want to do a substitution on HttpContext.Request.Body.

I've tried to do it inside a middleware:

public async Task Invoke(HttpContext context)
{
    if (context.Request.Path.Value.Contains("DataSourceResult"))
    {
        var originalBody = new StreamReader(context.Request.Body).ReadToEnd();
        DataSourceRequest dataSource = null;

        try
        {
            dataSource = JsonConvert.DeserializeObject<DataSourceRequest>(originalBody);
        } catch
        {
            await _next.Invoke(context);
        }

        if (dataSource != null && dataSource.Take > 2000)
        {
            dataSource.Take = 2000;

            var bytesToWrite = dataSource.AsByteArray();
            await context.Request.Body.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
        }
        else
        {
            var bytesToWrite = originalBody.AsByteArray();
            await context.Request.Body.WriteAsync(bytesToWrite, 0, bytesToWrite.Length);
        }
    }

    await _next.Invoke(context);
}

The first problem is that the body can be read only once, and secondly, the stream is read-only and can't be written to.

How can I modify/substitute Request.Body? I need to change property value of request body.

Pang
  • 9,564
  • 146
  • 81
  • 122
ovasylenko
  • 2,635
  • 4
  • 17
  • 29

2 Answers2

34

Take the request body, read its content, make whatever changes are necessary if at all, then create a new stream to pass down the pipeline. Once accessed, the request stream has to be replaced.

public async Task Invoke(HttpContext context) {
    var request = context.Request;
    if (request.Path.Value.Contains("DataSourceResult")) {
        //get the request body and put it back for the downstream items to read
        var stream = request.Body;// currently holds the original stream                    
        var originalContent = new StreamReader(stream).ReadToEnd();
        var notModified = true;
        try {
            var dataSource = JsonConvert.DeserializeObject<DataSourceRequest>(originalContent);
            if (dataSource != null && dataSource.Take > 2000) {
                dataSource.Take = 2000;
                var json = JsonConvert.SerializeObject(dataSource);
                //replace request stream to downstream handlers
                var requestContent = new StringContent(json, Encoding.UTF8, "application/json");
                stream = await requestContent.ReadAsStreamAsync();//modified stream
                notModified = false;
            }
        } catch {
            //No-op or log error
        }
        if (notModified) {
            //put original data back for the downstream to read
            var requestData = Encoding.UTF8.GetBytes(originalContent);
            stream = new MemoryStream(requestData);
        }

        request.Body = stream;
    }
    await _next.Invoke(context);
}
Pang
  • 9,564
  • 146
  • 81
  • 122
Nkosi
  • 235,767
  • 35
  • 427
  • 472
  • Why is `if (notModified) {` block required. The `request.Body` would be untouched if `notModified` is `true`, isn't it? – Sateesh Pagolu Sep 15 '21 at 09:10
  • @SateeshPagolu Because the code block before it can potentially modify the stream, changing `notModified` to `false`. Read the comments within the code. – Nkosi Sep 15 '21 at 11:05
2

In my case it was also necessery to update Request.ContentLength to new value. My solution is :

string originalContent;
using (StreamReader stream = new StreamReader(context.Request.Body))
{
    originalContent = stream.ReadToEnd();
}

var dataSource = JsonConvert.DeserializeObject<DataSourceRequest>(originalContent);
if (dataSource != null && dataSource.Take > 2000)
    dataSource.Take = 2000;

string json = JsonConvert.SerializeObject(dataSource);
var requestData = Encoding.UTF8.GetBytes(json);
context.Request.Body = new MemoryStream(requestData);
context.Request.ContentLength = context.Request.Body.Length;