From ae4702f5f0d9f4507e3de3f1d52628875be4ee35 Mon Sep 17 00:00:00 2001 From: Dustin Moris Gorski Date: Thu, 26 Nov 2020 22:13:32 +0000 Subject: [PATCH] Fixes #449 --- RELEASE_NOTES.md | 4 ++++ src/Giraffe/ModelBinding.fs | 19 +++++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/RELEASE_NOTES.md b/RELEASE_NOTES.md index 339fc2e3..de68c54e 100644 --- a/RELEASE_NOTES.md +++ b/RELEASE_NOTES.md @@ -1,6 +1,10 @@ Release Notes ============= +## 5.0.0-rc-3 + +- Added `ReadBodyBufferedFromRequestAsync` extension method to buffer and read a the request body and make subsequent reads possible (see [#449](https://github.com/giraffe-fsharp/Giraffe/issues/449)) + ## 5.0.0-rc-2 - Fixed pre-conditions validation issue (see [#424](https://github.com/giraffe-fsharp/Giraffe/issues/424)) diff --git a/src/Giraffe/ModelBinding.fs b/src/Giraffe/ModelBinding.fs index 6c047076..a14ce434 100644 --- a/src/Giraffe/ModelBinding.fs +++ b/src/Giraffe/ModelBinding.fs @@ -365,6 +365,25 @@ type HttpContext with return! reader.ReadToEndAsync() } + /// + /// Reads the entire body of the asynchronously and returns it as a value. + /// This method buffers the response and makes subsequent reads possible. + /// + /// Returns the contents of the request body as a . + member this.ReadBodyBufferedFromRequestAsync() = + task { + this.Request.EnableBuffering() + use reader = + new StreamReader( + this.Request.Body, + encoding = Encoding.UTF8, + detectEncodingFromByteOrderMarks = false, + leaveOpen = true) + let! body = reader.ReadToEndAsync() + this.Request.Body.Position <- 0L + return body + } + /// /// Uses the to deserializes the entire body of the asynchronously into an object of type 'T. ///