| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Retrieves a request body value from the HTTP request. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function accesses the current HTTP request context and retrieves the value |
| | | 6 | | of the request body. |
| | | 7 | | .PARAMETER Raw |
| | | 8 | | If specified, retrieves the raw request body without any parsing. |
| | | 9 | | .PARAMETER Type |
| | | 10 | | Specifies the type to which the request body should be deserialized. |
| | | 11 | | .EXAMPLE |
| | | 12 | | $value = Get-KrRequestBody |
| | | 13 | | Retrieves the value of the request body from the HTTP request. |
| | | 14 | | .EXAMPLE |
| | | 15 | | $value = Get-KrRequestBody -Raw |
| | | 16 | | Retrieves the raw request body from the HTTP request without any parsing. |
| | | 17 | | .OUTPUTS |
| | | 18 | | Returns the value of the request body, or $null if not found. |
| | | 19 | | .NOTES |
| | | 20 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 21 | | #> |
| | | 22 | | function Get-KrRequestBody { |
| | | 23 | | [KestrunRuntimeApi('Route')] |
| | | 24 | | [CmdletBinding()] |
| | | 25 | | [OutputType([Hashtable])] |
| | | 26 | | param( |
| | | 27 | | [switch]$Raw, |
| | | 28 | | [Type]$Type |
| | | 29 | | ) |
| | | 30 | | |
| | 0 | 31 | | if ($null -ne $Context.Request) { |
| | 0 | 32 | | $body = $Context.Request.Body |
| | 0 | 33 | | $contentType = $Context.Request.ContentType |
| | | 34 | | # Check if we need to read the stream (e.g., for compressed requests) |
| | 0 | 35 | | $hasEncoding = -not [string]::IsNullOrWhiteSpace($Context.Request.Headers['Content-Encoding']) |
| | 0 | 36 | | $bodyIsString = $body -is [string] |
| | 0 | 37 | | $needsStreamRead = $hasEncoding -and (-not $bodyIsString -or [string]::IsNullOrEmpty($body)) |
| | | 38 | | # Need to read the stream if not already read |
| | 0 | 39 | | if ($needsStreamRead) { |
| | 0 | 40 | | $reader = [System.IO.StreamReader]::new($Context.HttpContext.Request.Body, [System.Text.Encoding]::UTF8, $fa |
| | | 41 | | try { |
| | 0 | 42 | | $body = $reader.ReadToEndAsync().GetAwaiter().GetResult() |
| | | 43 | | } finally { |
| | 0 | 44 | | $reader.Dispose() |
| | | 45 | | } |
| | | 46 | | } |
| | | 47 | | # Return the raw body if specified |
| | 0 | 48 | | if ($Raw) { |
| | | 49 | | # Get the raw request body value from the request |
| | 0 | 50 | | return $body |
| | | 51 | | } |
| | | 52 | | # Parse the request body based on the specified type or content type |
| | 0 | 53 | | if ($null -ne $Type) { |
| | 0 | 54 | | return [Kestrun.Utilities.Json.JsonSerializerHelper]::FromJson($body, $Type) |
| | | 55 | | } |
| | | 56 | | # Parse based on Content-Type |
| | 0 | 57 | | switch ($contentType) { |
| | | 58 | | 'application/json' { |
| | 0 | 59 | | return $body | ConvertFrom-Json -AsHashtable |
| | | 60 | | } |
| | | 61 | | 'application/yaml' { |
| | 0 | 62 | | return [Kestrun.Utilities.YamlHelper]::ToHashtable( $body) |
| | | 63 | | } |
| | | 64 | | 'application/x-www-form-urlencoded' { |
| | 0 | 65 | | return $Context.Request.Form |
| | | 66 | | } |
| | | 67 | | 'application/xml' { |
| | 0 | 68 | | return [Kestrun.Utilities.XmlHelper]::ToHashtable( $body) |
| | | 69 | | } |
| | | 70 | | default { |
| | 0 | 71 | | return $body |
| | | 72 | | } |
| | | 73 | | } |
| | | 74 | | } |
| | | 75 | | } |