Resolved trying to read the request body?

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.

1696419800171.png


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();
            }

        }
 
I'm not seeing in the code where you are actually trying to get the ReadTimeout value.

If you are depending on what you are seeing from the debugging panes, be aware that pane does some reflection and gets values from objects in near real time. The object may not be in the appropriate state at the time the attempt to get the value. Or it may simply not be implemented, which I suspect maybe the case.

Notice that in the documentation, Body is declared to be a Stream class:

The default implementation of Stream does not implement ReadTimeout.
C#:
public virtual int ReadTimeout
{
    get => throw new InvalidOperationException(SR.InvalidOperation_TimeoutsNotSupported);
    set => throw new InvalidOperationException(SR.InvalidOperation_TimeoutsNotSupported);
}
 
Glad you solved it. In the future please do not remove your title. Just mark it as resolved. Now with the edited title, someone else who comes to this forum will have no idea what this thread was about.
 

Latest posts

Back
Top Bottom