Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
How to get the body of a HTTP Request using C#
private string GetDocumentContents(System.Web.HttpRequestBase Request)
{
string documentContents;
using (Stream receiveStream = Request.InputStream)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
documentContents = readStream.ReadToEnd();
}
}
return documentContents;
}
@ghost

This comment has been minimized.

Show comment Hide comment
@ghost

ghost Oct 22, 2013

nice!

ghost commented Oct 22, 2013

nice!

@mohammad786

This comment has been minimized.

Show comment Hide comment
@mohammad786

mohammad786 Jul 15, 2016

Thank you so much ... you saved my lots of time.

Thank you so much ... you saved my lots of time.

@Formalizer

This comment has been minimized.

Show comment Hide comment
@Formalizer

Formalizer Jan 27, 2017

very nice snippet!

very nice snippet!

@Marklsq

This comment has been minimized.

Show comment Hide comment
@Marklsq

Marklsq Feb 15, 2017

Thank you,but how to get the full body has finished loading the JS code.

Marklsq commented Feb 15, 2017

Thank you,but how to get the full body has finished loading the JS code.

@bgurmendi

This comment has been minimized.

Show comment Hide comment
@bgurmendi

bgurmendi Apr 5, 2017

You sould use the Request.ContentEncoding

private string GetDocumentContents(HttpRequestBase Request)
        {
            string documentContents;
            using (Stream receiveStream = Request.InputStream)
            {
                using (StreamReader readStream = new StreamReader(receiveStream, Request.ContentEncoding))
                {
                    documentContents = readStream.ReadToEnd();
                }
            }
            return documentContents;
        }

You sould use the Request.ContentEncoding

private string GetDocumentContents(HttpRequestBase Request)
        {
            string documentContents;
            using (Stream receiveStream = Request.InputStream)
            {
                using (StreamReader readStream = new StreamReader(receiveStream, Request.ContentEncoding))
                {
                    documentContents = readStream.ReadToEnd();
                }
            }
            return documentContents;
        }
@tvcsharp

This comment has been minimized.

Show comment Hide comment
@tvcsharp

tvcsharp Jun 14, 2017

I am using this code inside a specflow step definiton to capture the body of http request like below.
but I am getting error. Can anybody help?
binding error: Parameter count mismatch! The binding method 'TestFeature.WhenBrowserPayLoadIsExtracted(HttpRequestBase)' should have 0 parameters

[When(@"Browser payload is extracted")]
private string WhenBrowserPayLoadIsExtracted(HttpRequestBase Request)
{
string documentContents;
using (Stream receiveStream = Request.InputStream)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
documentContents = readStream.ReadToEnd();
}
}
return documentContents;

    }

I am using this code inside a specflow step definiton to capture the body of http request like below.
but I am getting error. Can anybody help?
binding error: Parameter count mismatch! The binding method 'TestFeature.WhenBrowserPayLoadIsExtracted(HttpRequestBase)' should have 0 parameters

[When(@"Browser payload is extracted")]
private string WhenBrowserPayLoadIsExtracted(HttpRequestBase Request)
{
string documentContents;
using (Stream receiveStream = Request.InputStream)
{
using (StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8))
{
documentContents = readStream.ReadToEnd();
}
}
return documentContents;

    }
@yushifeiyang

This comment has been minimized.

Show comment Hide comment
@yushifeiyang

yushifeiyang Apr 16, 2018

thanks

thanks

@thyams

This comment has been minimized.

Show comment Hide comment
@thyams

thyams Apr 19, 2018

public async static Task<string> GetBodyContentAsStringAsync(HttpRequestBase request)
{
    string content = string.Empty;

    using (Stream receiveStream = request.InputStream)
    using (StreamReader readStream = new StreamReader(receiveStream, request.ContentEncoding))
    { content = await readStream.ReadToEndAsync(); }

    return content;
}

Thanks, and modernized.

Also, depending on your system this is avaliable:

var rawMessage = await Request.Content.ReadAsStringAsync();

thyams commented Apr 19, 2018

public async static Task<string> GetBodyContentAsStringAsync(HttpRequestBase request)
{
    string content = string.Empty;

    using (Stream receiveStream = request.InputStream)
    using (StreamReader readStream = new StreamReader(receiveStream, request.ContentEncoding))
    { content = await readStream.ReadToEndAsync(); }

    return content;
}

Thanks, and modernized.

Also, depending on your system this is avaliable:

var rawMessage = await Request.Content.ReadAsStringAsync();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment