WordPress WooCommerce ASP.net API WebHookHandler: The WebHook request must contain an entity body formatted as HTML Form Data WordPress WooCommerce ASP.net API WebHookHandler: The WebHook request must contain an entity body formatted as HTML Form Data wordpress wordpress

WordPress WooCommerce ASP.net API WebHookHandler: The WebHook request must contain an entity body formatted as HTML Form Data


Your WebHookReceiver is wrong

There is a mismatch of expecting HTML Form Data, when in fact it should be expecting JSON.

WordPressWebHookHandler is still the default

This is what is causing your error. If you look at the WordPressWebHookReceiver, the ReceiveAsync() method implementation, calls out to ReadAsFormDataAsync() method, which is not what you want, as your Content-Type is json. So, you want to be doing ReadAsJsonAsync().

Solution: Don't use the WordPressWebHookReceiver and switch it to another one that will call ReadAsJsonAsync().


Looking at the code

I am thinking maybe I need a custom WebHook instead of the WordPress specific one as this is a WooCommerce Webhook.

You had the right idea, so I dug up some of the code to explain exactly why this was happening.

The code block below is the ReceiveAsync() method that is overridden in the WordPressWebHookReceiver. You can see that it is calling the ReadAsFormDataAsync() which is not what you want...

public override async Task<HttpResponseMessage> ReceiveAsync(    string id, HttpRequestContext context, HttpRequestMessage request){    ...    if (request.Method == HttpMethod.Post)    {        // here is what you don't want to be called        // you want ReadAsJsonAsync(), In short, USE A DIFFERENT RECEIVER.        NameValueCollection data = await ReadAsFormDataAsync(request);        ...    }    else    {       return CreateBadMethodResponse(request);    }}

A quick search through the repository for classes that call the ReadAsJsonAsync() method, shows that the following recievers implement it:

  1. DynamicsCrmWebHookReceiver
  2. ZendeskWebHookReceiver
  3. AzureAlertWebHookReceiver
  4. KuduWebHookReceiver
  5. MyGetWebHookReceiver
  6. VstsWebHookReceiver
  7. BitbucketWebHookReceiver
  8. CustomWebHookReceiver
  9. DropboxWebHookReceiver
  10. GitHubWebHookReceiver
  11. PaypalWebHookReceiver
  12. StripeWebHookReceiver
  13. PusherWebHookReceiver

I assumed that the CustomWebHookReceiver would fit your requirements, so can grab the NuGet here. Otherwise you can implement your own, or derive it from this class, etc.


Configuring a WebHook Recevier

(Copied from the Microsoft Documentation)

Microsoft.AspNet.WebHooks.Receivers.Custom provides support for receiving WebHooks generated by ASP.NET WebHooks

Out of the box you can find support for Dropbox, GitHub, MailChimp, PayPal, Pusher, Salesforce, Slack, Stripe, Trello, and WordPress but it is possible to support any number of other providers

Initializing a WebHook Receiver

WebHook Receivers are initialized by registering them, typically in the WebApiConfig static class, for example:

public static class WebApiConfig{    public static void Register(HttpConfiguration config)    {        ...        // Load receivers        config.InitializeReceiveGitHubWebHooks();    }}


There is a problem with the data format that you send in your request. You must use format of HTML Form as your error message said.

Proper POST data format is described here: How are parameters sent in an HTTP POST request?

Don't forget to set Content-Length header and correct Content-Type if your library doesn't do it. Usually the content type is application/x-www-form-urlencoded.


I would like to make some additions to Svek's answer as I now got my Proof-of-concept completed and understand a bit more about the receivers.

His answer pointed me in the right direction, but needs a little addition.

WordpressWebHookReceiverCan take in Wordpress Webhooks of type HttpPost. This does not work with Woocommerce as Woocommerce sends Json Webhook messages and will fail the HttpPost validation which is build into the WordpressWebHookReceiver class.

CustomWebHookReceiverCan take in custom ASP.NET Webhooks. The custom ASP.NET webhooks have a specific partner for validation which includes but is not limited to the 'ms-signature'. Even adding the header will not suffice as the signature is also used in a different way from out of the box Woocommerce to encrypt the message. Basically coming to a point that you can't integrate Woocommerce with the CustomWebHookReceiver without changing the Webhook classes of Woocommerce.

GenericWebHookReceiverThis is the receiver you want, which accepts basically a generic set of Json data and will be able to use the "code" query parameter to verify the secret which you can add in the web.config of your asp.net api application. I used this receiver to finish the Proof-of-concept and got both the signature validation as well as the deciphering of the message working right of the bat.

My basic class which I will start to build into a real solution can be viewed below and changes the JObject into a dynamic object in the methods I call from the class. As you can see I have two methods currently added, one for the customer create and one for the order create to call the respective methods which do an insert into Dynamics 365 (former CRM).

public class GenericJsonWebHookHandler : WebHookHandler{    public GenericJsonWebHookHandler()    {        this.Receiver = "genericjson";    }    public override Task ExecuteAsync(string generator, WebHookHandlerContext context)    {        var result = false;        try        {            // Get JSON from WebHook            var data = context.GetDataOrDefault<JObject>();            if(context.Id != "crcu" && context.Id != "cror")                return Task.FromResult(true);            if (context.Id == "crcu")            {                result = WoocommerceCRMIntegrations.Entities.Contact.CreateContactInCRM(data);            }            else if (context.Id == "cror")            {                result = WoocommerceCRMIntegrations.Entities.Order.CreateOrderInCRM(data);            }        }        catch (Exception ex)        {            result = false;        }        return Task.FromResult(result);    }}