How to get raw request body in ASP.NET? How to get raw request body in ASP.NET? asp.net asp.net

How to get raw request body in ASP.NET?


The request object is not populated in the BeginRequest event. You need to access this object later in the event life cycle, for example Init, Load, or PreRender. Also, you might want to copy the input stream to a memory stream, so you can use seek:

protected void Page_Load(object sender, EventArgs e){    MemoryStream memstream = new MemoryStream();    Request.InputStream.CopyTo(memstream);    memstream.Position = 0;    using (StreamReader reader = new StreamReader(memstream))    {        string text = reader.ReadToEnd();    }}


Pål's answer is correct, but it can be done much shorter as well:

string req_txt;using (StreamReader reader = new StreamReader(HttpContext.Current.Request.InputStream)){    req_txt = reader.ReadToEnd();}

This is with .NET 4.6.


In ASP.NET Core 2:

using (var reader = new StreamReader(HttpContext.Request.Body)) {    var body = reader.ReadToEnd();}