locked
Modify the Request Body with Middleware RRS feed

  • Question

  • User1634889493 posted

    I found a lot of information on the web about how to modify response content, but is it possible to modify the request body on the way in with middleware? Can I replace the body with my own version and then send it on to the later middleware components?

    Thank You

    Thursday, August 22, 2019 8:22 PM

Answers

  • User-474980206 posted

    sure. in the typical middleware its:

    public async Task Invoke(HttpContext httpContext)
    {
       context.Request.Body = MyConvertBodyStream(context.Request.Body);  
       await _next(httpContext);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2019 8:57 PM
  • User711641945 posted

    Hi Blown2Bytes,

    is it possible to modify the request body on the way in with middleware?

    Yes,it is.Here is a workaroud about how to modify the request body:

    public async Task Invoke(HttpContext context)
    {
           var request = context.Request;
           //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<TestVM>(originalContent);
                 if (dataSource != null)
                 {
                    //modified stream
                    var modifydata = new TestVM() { Name = "vbb", Hobby = "sss" };
                    var json = JsonConvert.SerializeObject(modifydata);
                    var requestData = Encoding.UTF8.GetBytes(json);
                    stream = new MemoryStream(requestData);
                    notModified = false;
                }
              }
           catch
           {
               //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);
    }
    

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2019 9:47 AM

All replies

  • User-474980206 posted

    sure. in the typical middleware its:

    public async Task Invoke(HttpContext httpContext)
    {
       context.Request.Body = MyConvertBodyStream(context.Request.Body);  
       await _next(httpContext);
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, August 22, 2019 8:57 PM
  • User711641945 posted

    Hi Blown2Bytes,

    is it possible to modify the request body on the way in with middleware?

    Yes,it is.Here is a workaroud about how to modify the request body:

    public async Task Invoke(HttpContext context)
    {
           var request = context.Request;
           //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<TestVM>(originalContent);
                 if (dataSource != null)
                 {
                    //modified stream
                    var modifydata = new TestVM() { Name = "vbb", Hobby = "sss" };
                    var json = JsonConvert.SerializeObject(modifydata);
                    var requestData = Encoding.UTF8.GetBytes(json);
                    stream = new MemoryStream(requestData);
                    notModified = false;
                }
              }
           catch
           {
               //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);
    }
    

    Best Regards,

    Rena

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, August 23, 2019 9:47 AM