| | 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 | | .EXAMPLE |
| | 10 | | $value = Get-KrRequestBody |
| | 11 | | Retrieves the value of the request body from the HTTP request. |
| | 12 | | .EXAMPLE |
| | 13 | | $value = Get-KrRequestBody -Raw |
| | 14 | | Retrieves the raw request body from the HTTP request without any parsing. |
| | 15 | | .OUTPUTS |
| | 16 | | Returns the value of the request body, or $null if not found. |
| | 17 | | .NOTES |
| | 18 | | This function is designed to be used in the context of a Kestrun server response. |
| | 19 | | #> |
| | 20 | | function Get-KrRequestBody { |
| | 21 | | [KestrunRuntimeApi('Route')] |
| | 22 | | [CmdletBinding()] |
| | 23 | | [OutputType([Hashtable])] |
| | 24 | | param( |
| | 25 | | [switch]$Raw |
| | 26 | | ) |
| 0 | 27 | | if ($null -ne $Context.Request) { |
| 0 | 28 | | if ($Raw) { |
| | 29 | | # Get the raw request body value from the request |
| 0 | 30 | | return $Context.Request.Body |
| | 31 | | } |
| 0 | 32 | | switch ($Context.Request.ContentType) { |
| | 33 | | 'application/json' { |
| 0 | 34 | | return $Context.Request.Body | ConvertFrom-Json -AsHashtable |
| | 35 | | } |
| | 36 | | 'application/yaml' { |
| 0 | 37 | | return [Kestrun.Utilities.YamlHelper]::ToHashtable( $Context.Request.Body) |
| | 38 | | } |
| | 39 | | 'application/x-www-form-urlencoded' { |
| 0 | 40 | | return $Context.Request.Form |
| | 41 | | } |
| | 42 | | 'application/xml' { |
| 0 | 43 | | return [Kestrun.Utilities.XmlHelper]::ToHashtable( $Context.Request.Body) |
| | 44 | | } |
| | 45 | | default { |
| 0 | 46 | | return $Context.Request.Body |
| | 47 | | } |
| | 48 | | } |
| | 49 | | } |
| | 50 | | } |
| | 51 | |
|