Campaign Monitor API via .NET

1.2k Views Asked by At

Has anyone used the Campaign Monitor to receive subscription updates via the Campaign Monitor webhooks? I have configured the webhook to post json data to a page on our website, however when my code receives the data it is just a bunch of zero's. My code is as follows:

System.IO.Stream stream = Request.InputStream;
        byte[] byteData = new byte[Convert.ToInt32(stream.Length)];
        int read = stream.Read(byteData, 0, Convert.ToInt32(stream.Length));
        string streamData = string.Empty;
        string somedata = System.Text.Encoding.ASCII.GetString(byteData).ToString();
        for (int i = 0; i < Convert.ToInt32(stream.Length); i++)
        {
            streamData = streamData + byteData[i].ToString();
        }

I've tried posting data to my page and it works - i.e. the code above (somedata) returns the same text value that I posted to the page. Very frustrating....

Any ideas anyone? I've lost lots of hair tonight!!!

Thanks in advance Al

1

There are 1 best solutions below

0
On

You can read the body of the request as follows (from a webforms app):

var reader = new StreamReader(Request.InputStream);
var json = reader.ReadToEnd();

The docs specify that the data you're expecting for the subscribe event is of the form:

{
    "Events": [
        {
            "CustomFields": [
                {
                    "Key": "website", 
                    "Value": "http:\/\/example.org"
                }
            ],
            "Date": "2010-12-14 11:32:00",
            "EmailAddress": "[email protected]",
            "Name": "Test Subscriber",
            "SignupIPAddress": "53.78.123.243",
            "Type": "Subscribe"
        }
    ],
    "ListID": "96c0bbdaa54760c8d9e62a2b7ffa2e13"
}

You could test this out yourself by submitting POST requests to your URL using the example data above. Here's an example using curl:

curl http://localhost:49681/receiver.aspx -v -X POST -d \
'{
    "Events": [
        {
            "CustomFields": [
                {
                    "Key": "website", 
                    "Value": "http:\/\/example.org"
                }
            ],
            "Date": "2010-12-14 11:32:00",
            "EmailAddress": "[email protected]",
            "Name": "Test Subscriber",
            "SignupIPAddress": "53.78.123.243",
            "Type": "Subscribe"
        }
    ],
    "ListID": "96c0bbdaa54760c8d9e62a2b7ffa2e13"
}'