I've got an action on a controller that is used to process requests from a payment gateway. They send json in the request's body to a url on my server that executes this action. This code is working fine when they send their data:
[ValidateInput(false)]
public ActionResult WebHookHandler()
{
var json = new StreamReader(Request.InputStream).ReadToEnd();
if (string.IsNullOrEmpty(json))
return new HttpStatusCodeResult(400); // bad request
...
// return ok status
return new HttpStatusCodeResult(200); // ok
}
The problem is I want to test this action on my end by submitting test data to it but I can't get it to work. Everything I've tried results in a 400 response which means the json I sent wasn't extracted on the server side. I think MVC 3 is trying to be too smart and is handling the json I'm sending it in a manner that doesn't allow it to be retrieved from the Request.InputStream property because that property is always empty on any ajax configuration I've tried.
I've tried all kinds of combinations like stringify the data, setting the processData to false, different contentType's and nothing is letting the data go through in a manner that allows my server-side code to grab it from the Request.InputStream.
Here is what my javascript looks like:
var data = $("#stripeJSON").val();
$.ajax({
url: "http://localhost/PaymentStripe/WebHookHandler",
type: "POST",
data: data,
processData: true,
contentType: "application/json",
success: function (data, textStatus, jqXHR) {
$("#result").html("success");
},
error: function (jqXHR, textStatus, errorThrown) {
$("#result").html("failed:<br/>" + textStatus + errorThrown);
},
complete: function (jqXHR, textStatus) {
}
});
And here is some dummy json data:
{
"pending_webhooks": 1,
"type": "invoice.payment_succeeded",
"object": "event",
"created": 1347318097,
"livemode": false,
"id": "evt_0LMvt7Q9vL1oFI",
"data": {
"object": {
"currency": "usd",
"ending_balance": null,
"customer": "cus_0LMvSw8LEmOcJG",
"discount": null,
"id": "in_0LMvHGx1XutT7p",
"object": "invoice",
"amount_due": 0,
"date": 1347318097,
"total": 0,
"subtotal": 0,
"charge": null,
"period_end": 1347318097,
"next_payment_attempt": null,
"livemode": false,
"attempted": true,
"period_start": 1347318097,
"starting_balance": 0,
"lines": {
"subscriptions": [{
"quantity": 1,
"period": {
"end": 1349910097,
"start": 1347318097
},
"amount": 0,
"plan": {
"livemode": false,
"trial_period_days": null,
"amount": 0,
"object": "plan",
"name": "ZeroMonthly",
"id": "ZeroMonthly",
"interval_count": 1,
"currency": "usd",
"interval": "month"
}
}],
"prorations": [],
"invoiceitems": []
},
"paid": true,
"closed": true,
"attempt_count": 0
}
}
}
Suggestions?