Question

I'm trying to get the form data being posted by Amazon SNS. An example of a request they make is given here.

My page is an ASHX handler. I've tried:

context.Request.Form.ToString();

context.Request.ServerVariables["ALL_RAW"]

var data = String.Empty;
if (context.Request.ContentLength > 0)
{
    using (var requestStream = new MemoryStream())
    {
        var buffer = new byte[context.Request.ContentLength];
        context.Request.InputStream.Read(buffer, 0, context.Request.ContentLength);
        requestStream.Write(buffer, 0, context.Request.ContentLength);
        data = new StreamReader(requestStream).ReadToEnd();
    }
}

All of these return nothing, if I catch the request in my logs using those above I can only seem to capture the header of the request and not the actual data being posted itself.

How do I capture this JSON data being supposedly posted into a string?

Was it helpful?

Solution

Bah, always seem to solve these as soon as I post them! This seems to get the data fine:

var data = String.Empty;
using (var receiveStream = context.Request.InputStream)
{
    using (var readStream = new StreamReader(receiveStream, Encoding.UTF8))
    {
        data = readStream.ReadToEnd();
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top