CarterCommunity / Carter

Carter is framework that is a thin layer of extension methods and functionality over ASP.NET Core allowing code to be more explicit and most importantly more enjoyable.
MIT License
2.14k stars 177 forks source link

Add EnableRewind option to AsString body extension #156

Closed jchannon closed 2 years ago

jchannon commented 5 years ago

This will need to call 👍

 private static async Task PrepareRequestBody(HttpContext context)
        {
            if (!context.Request.Body.CanSeek)
            {
                context.Request.EnableBuffering();

                await context.Request.Body.DrainAsync(CancellationToken.None);
            }

            // Always start at the beginning.
            context.Request.Body.Seek(0L, SeekOrigin.Begin);
        }

Then read the body without a using statement in a StreamReader otherwise this closes the stream and it can't be read again therefore you'd need new StreamReader(stream).ReadToEndAsync()

jchannon commented 5 years ago

See here for usage - http://github.com/LykkeCorp/apollo/blob/e826f0ae9f79f85b1bc24ff475d185d6daa05787/src/Apollo/Features/IdentityVerification/Identity/WebhookModule.cs#L39

0x414c49 commented 4 years ago

@jchannon the link you provided does not exist anymore.

jchannon commented 4 years ago

Ah its just a link to show how you could call AsString() which will have the code above in and then you could call Bind and it wouldn't fail as the body's position would be back at position 0

jchannon commented 2 years ago

Closing, not really an issue as we don't have Bind anymore