Question

Webhook notification callback C#

  • 9 October 2019
  • 2 replies
  • 2481 views

Hi Phong, my webhook is setup and working, I can see event notifications in the webhook server console. My question is how do I retrieve the notification payload on the client so I can handle it? SubcriptionInfo (type returned by Subscription.Post(new CreateNew SubscriptionRequest{...})) does not have a Response property. See code snippet from client below:


2 replies

Userlevel 1

The code you posted above is just for subscribing for the notification. The Webhook callback is running on the server code below and you can parse the body as shown in bold text:

using System;
using System.IO;
using System.Text;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Primitives;

namespace webhook_server
{
    public class Startup
    {
        public void ConfigureServices(IServiceCollection services)
        {
        }

        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment()) app.UseDeveloperExceptionPage();

            app.Run( async (context) =>
            {
                context.Request.Headers.TryGetValue("Validation-Token", out StringValues validationToken);
                context.Response.Headers.Add("Validation-Token", validationToken);
                if (context.Request.Path == "/webhook" && context.Request.Method == "POST")
                {
                    using (StreamReader reader = new StreamReader(context.Request.Body, Encoding.UTF8))
                    {
                        var str = reader.ReadToEnd();
                        Console.WriteLine(str);
                    }
                }
            });
        }
    }
}


My understanding is that the server code you show above gets called once, when the subscription request is created. From what I can see (through breakpoints), the server reads the request object from the client. Are you saying the notifications are also stored in context.response on the webhook server?

Reply