I am experimenting with OWIN middleware in an Web API application and I wish to intercept the request in the pipeline and decode the request body in flight before the request gets to the API Controller/Action.
I have taken a json string and base 64 encoded it as part of this test. I am able to intercept the request, read the body text, decode it as to get the original string but I am unable to update the request body with the new data. Here is a code snippet:
public override async Task Invoke(IOwinContext context)
string body = new StreamReader(context.Request.Body).ReadToEnd();
byte data = Convert.FromBase64String(body);
string decodedString = Encoding.UTF8.GetString(data);
context.Request.ContentType = "application/json";
context.Request.Body = new MemoryStream(Encoding.UTF8.GetBytes(decodedString));
This code executes without issue and I can see decoded string value having the correct data. The kicker is that when the request moves on to the controller / action, the body is that of the old data / encoded string.
Has anyone tried to intercept the api request via owin and replace it with something other than what was originally posted?
Sample controller / action snipped used to pull the value from the request body:
public void Post([FromBody]string value)
var req = Request;
var x = value;
var y = x;
Thanks in advance!