Reputation: 19378
I'm trying to integrate with stripe.com and webhooks are giving me an issue.
I've created a controller and an action to accept and parse the webhooks from stripe:
public class WebhooksController : AbstractController
{
public JsonResult Stripe(int id)
{
// some parsing code
}
}
This is routed via
routes.MapRoute("webhooks", "Financial/Webhooks/{action}/{id}", new { "Webhooks", "Index", id = "" }, new string[] { "MyApp.Controllers.Financial" });
I can hit this route fine from the browser or with
curl -i -H "Host: mydomain.com" -H "Content-Type: application/json" -X POST http://{resolvedIP}/Financial/Webhooks/Stripe/4
However, when I try to fire the webhook from stripes dashboard using the "test webhook" feature, I get a 500:
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.JsonResult Stripe(Int32)' in 'Drg.M3.Client.Controllers.Financial.WebhooksController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
According to one staff member in the stripe chatroom the webhooks are sent with something like this:
curl -i -H "Host: mydomain.com" -H "Content-Type: application/json" -X POST http://{resolvedIP}/Financial/Webhooks/Stripe/4 -d '{big ol' json object}'
Why would a request with data fail at routing when one without the data goes through?
Upvotes: 0
Views: 1680
Reputation: 19378
Solved.
Turns out, that when MVC sees a content type of application/json
it tries to auto bind the data to your action parameters.
Since the json object Stripe sends includes
{
"id" : "evt_0000000000",
...
}
The id
parameter was getting set to evt_0000000000
instead of 4
.
Upvotes: 1