We have a requirement in which for a certain rest call, we need to intercept the request before getting it to the controller and add something to the request body. I am able to do that using an authorization filter. The problem is, when I read the request using stream reader, it somehow adds an extra slash to '\t' (tab). That makes it '\t'. When the request gets to controller, due to this extra slash, it is treated as text instead of 'tab'.
Request Body
{
"myData": "This is some text \t more text"
}
Controller
[MyRequestConverter]
[HttpPost]
public virtual IActionResult myServiceAction([FromBody]myRequestModel myRequest)
{
// suppose print is something that writes the data to a file
print(myRequest.myData);
}
Authorization Filter
public class MyRequestConverter : Attribute, IAsyncAuthorizationFilter
{
public MyRequestConverter() { }
private async Task<string> GetRequestBodyAsTextAsync(HttpRequest request)
{
var reqbody = "";
request.EnableBuffering();
request.Body.Seek(0, SeekOrigin.Begin);
using (var reader = new StreamReader(request.Body, Encoding.UTF8, true, 1024, true))
{
reqbody = await @reader.ReadToEndAsync();
}
request.Body.Position = 0;
return reqbody;
}
public async Task OnAuthorizationAsync(AuthorizationFilterContext filterContext)
{
var stream = request.Body;// currently holds the original stream
var originalContent = await GetRequestBodyAsTextAsync(request);
originalContent = AddSomethingToRequestBody(originalContent);
var requestContent = new StringContent(originalContent, Encoding.UTF8, "application/json");
stream = await requestContent.ReadAsStreamAsync();
var requestData = Encoding.UTF8.GetBytes(originalContent);
stream = new MemoryStream(requestData);
filterContext.HttpContext.Request.Body = stream;
}
}
With 'MyRequestConverter', it prints (it treats \t as text due to that extra slash)
This is some text \t more text
Without 'MyRequestConverter', it prints (desired output - it has tab spacing)
This is some text (tab) more text
I did some search and looks like one option is to replace '\\t' with '\t' but is there any other solution to this problem ?
Use JsonDocument.Parse(originalContent) so it will be pause to the json again. and in the end it will be output like the desired output i.e. This is some text (tab) more text
Here is the test code