TouchStarDev
TouchStarDev

Reputation: 441

Asp.net Webforms: Create webhook receiver for typeform

How I can make a webhook receiver with asp.net web forms for Typeform and how will i get data on my app whenever someone submits my form.

Upvotes: 2

Views: 1719

Answers (1)

Mark
Mark

Reputation: 475

To expose an endpoint to receive a POST request, I would create a HTTP handler, in the asp.net world, known as a "Generic Web handler", which is a file with a .ashx extension.

You can see a guide on how to create one here: https://briancaos.wordpress.com/2009/02/13/the-ashx-extension-writing-your-own-httphandler/

The implementation could look something like this:

using System.Web;
using Newtonsoft.Json.Linq; // From https://www.newtonsoft.com/json

namespace MyNamespace
{
  public class MyClass : IHttpHandler
  {
    public void ProcessRequest(HttpContext context)
    {
      string body = String.Empty;
      context.Request.InputStream.Position = 0;

      using (var inputStream = new StreamReader(context.Request.InputStream))
      {
          body = inputStream.ReadToEnd();
      }

      dynamic json = JObject.Parse(body);

      // Access the webhook payload data ie, get first answer:
      var answers = json.form_response.answers;
      Console.WriteLine(answers)

      context.Response.StatusCode = 200;
      context.Response.End();
    }

    public bool IsReusable
    {
      get { return true; }
    }
  }
}

You can find a complete overview of different HTTP handlers here: https://msdn.microsoft.com/en-us/library/bb398986.aspx?f=255&MSPPError=-2147217396

Upvotes: 5

Related Questions