totoman
New member
- Joined
- Oct 4, 2023
- Messages
- 2
- Programming Experience
- 1-3
Hello, I'm trying to implement Stripe API into my website, below code is where I try to create a webhook and listen to Stripe API but I keep getting this error when trying to read the request body.
ReadTimeout = '((Microsoft.AspNetCore.Http.DefaultHttpRequest)HttpContext.Request).Body.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
The program runs normally but the json is always empty, when I use breakpoint and debug this pops out.
ReadTimeout = '((Microsoft.AspNetCore.Http.DefaultHttpRequest)HttpContext.Request).Body.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
The program runs normally but the json is always empty, when I use breakpoint and debug this pops out.
C#:
[System.Web.Http.HttpPost]
public async Task<IActionResult> WebHook()
{
HttpContext.Request.Body.Position = 0;
var json = await new StreamReader(HttpContext.Request.Body).ReadToEndAsync();
try
{
var stripeEvent = EventUtility.ConstructEvent(
json,
Request.Headers["Stripe-Signature"],
_stripeSettings.WHSecret);
if (stripeEvent.Type == Events.CheckoutSessionCompleted)
{
// Send Code via email
System.Diagnostics.Debug.WriteLine("Send Code");
}
else if (stripeEvent.Type == Events.CheckoutSessionAsyncPaymentFailed)
{
System.Diagnostics.Debug.WriteLine("payment failed");
return View("Fail");
}
else
{
System.Diagnostics.Debug.WriteLine("Unhandled event type: {0}", stripeEvent.Type);
return View("Cancel");
}
HttpContext.Request.Body.Position = 0;
return Ok();
}
catch (StripeException e)
{
Console.WriteLine(e.StripeError.Message);
return BadRequest();
}
}