question

Ivie O avatar image
Ivie O asked ·

Webhook notification callback C#

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:

notification payload
1570621022651.png (29.2 KiB)
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Phong Vu avatar image
Phong Vu answered ·

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);
                    }
                }
            });
        }
    }
}


Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Ivie O avatar image
Ivie O answered ·

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?

4 comments Share
1 |1000 characters needed characters left characters exceeded

Up to 8 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.

Correct, the server gets called immediately after you subscribed to validate the token. You must keep the server running so it will get called again when there is a notification.

0 Likes 0 · ·

So is there a way to return context,request.body (the notification payload) back to my client so that I can handle the event there, (in other words, where the subscription was requested)? It seems to me that the server should not be concerned with specific event handling or how the payload will be used.

0 Likes 0 · ·

Webhooks cannot push anything directly to your browser client app. That is a common implementation for client/server communications for different Web frameworks. You can check out WebSockets. Read the answers of these threads from stackoverflow

https://stackoverflow.com/questions/34586733/sending-a-value-from-server-to-client-with-sockets

https://stackoverflow.com/questions/12304960/implementing-a-client-side-webhook-handler

Or, you can turn to use the PubNub notification.

0 Likes 0 · ·
Show more comments

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MiB each and 10.0 MiB total.