user2568584
user2568584

Reputation: 31

How do you create a web hook handler in Asp.net MVC 4?

My issue is that I do not know how to process an incoming request without knowing the URL that the post message is coming from.

The Vend API that I am trying to integrate with sends a post message to my URL with the following information:

"The POST body will include a field named payload. This field contains a JSON encoded object with details of the object that caused the hooked event. Other form fields (such as environment or domain_prefix) may be present, but are not guaranteed to be.

The payload objects you’ll find in webhook requests are now the same as those you’ll receive from the newer parts of the API marked as version 1.0 or higher. So, for example, the product webhook should give you a product payload that’s the same as if you requested /api/1.0/product/{product_id}."

http://docs.vendhq.com/webhooks.html (here is the link for more details)

I'm fairly new to ASP MVC and I'm having trouble figuring out the best way to go forth. I need to eventually map the incoming name value pairs to my model.

Any help would be greatly appreciated.

Upvotes: 3

Views: 2078

Answers (1)

Matthew
Matthew

Reputation: 957

When you said you don't know the URL that the post message is coming from, I think you mean that you don't know the format of the URL (path and parameters) that the webhook is being posted to. The only things your MVC app needs to know in order to process the request are the names of the post parameters, which in this case is a single parameter named payload that contains a JSON string according to the snippet you included from the documentation.

There are several ways for you to get this data. One of the most simple is to take advantage of ASP.NET MVC's automatic mapping of POST parameters to controller action method parameters:

public ActionResult MyActionMethod(string payload)
{ 
   // 'payload' will be automatically populated with the json string from the POST payload
}

Of course you now have to parse the json payload which you can do in several ways using the .NET framework or with any number of 3rd party libraries.

Upvotes: 1

Related Questions