How do I get the raw request body from the Request.Content object using .net 4 api endpoint

后端 未结 4 1469
北海茫月
北海茫月 2020-12-09 15:02

I\'m trying to capture the raw request data for accountability and want to pull the request body content out of the Request object.

I\'ve seen suggestions doing a Re

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 15:40

    In your comment on @Kenneth's answer you're saying that ReadAsStringAsync() is returning empty string.

    That's because you (or something - like model binder) already read the content, so position of internal stream in Request.Content is on the end.

    What you can do is this:

    public static string RequestBody()
    {
        var bodyStream = new StreamReader(HttpContext.Current.Request.InputStream);
        bodyStream.BaseStream.Seek(0, SeekOrigin.Begin);
        var bodyText = bodyStream.ReadToEnd();
        return bodyText;
    }
    

提交回复
热议问题