I need to read the content sent via a post/put to my Web API via an attribute to perform some additional validation but the content value is always empty even thought I can see the context-size
is set a value i.e. 2067 and the content-type
is set to application/json
I've tried different things but none seem to work:
- ReadAsync
- ReadAsByteArrayAsync
- ReadAsStringAsync
- etc...
My last attempt looks as follows:
public async override void OnActionExecuting(HttpActionContext actionContext)
{
using (MemoryStream ms = new MemoryStream())
{
await actionContext.Request.Content.CopyToAsync(ms)
.ConfigureAwait(false);
var str = System.Text.UTF8Encoding.
UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);
}
}
The reason I tried to use the CopyTo is that I noticed that when I called some of the above functions, that even though they returned an empty string, I could make that call once is after some googling, I believe this is by design.
Bottom line is that I need to access the body/content of my request so that I can inspect the json that was sent.
Thanks.