| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Writes key/value data to the HTTP response body as application/x-www-form-urlencoded. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Uses the ASP.NET Core built-in System.Net.Http.FormUrlEncodedContent encoder |
| | | 6 | | to convert the provided object into an application/x-www-form-urlencoded |
| | | 7 | | payload, then writes it to the HTTP response. |
| | | 8 | | .PARAMETER InputObject |
| | | 9 | | Hashtable, PSCustomObject, dictionary, or any object with public properties. |
| | | 10 | | .PARAMETER StatusCode |
| | | 11 | | HTTP status code for the response. |
| | | 12 | | .PARAMETER ContentType |
| | | 13 | | Defaults to 'application/x-www-form-urlencoded'. |
| | | 14 | | .EXAMPLE |
| | | 15 | | @{ user='alice'; role='admin' } | Write-KrFormUrlEncodedResponse |
| | | 16 | | #> |
| | | 17 | | function Write-KrFormUrlEncodedResponse { |
| | | 18 | | [KestrunRuntimeApi('Route')] |
| | | 19 | | [CmdletBinding()] |
| | | 20 | | param( |
| | | 21 | | [Parameter(Mandatory = $true, ValueFromPipeline = $true)] |
| | | 22 | | [object]$InputObject, |
| | | 23 | | |
| | | 24 | | [Parameter()] |
| | | 25 | | [int]$StatusCode = 200 |
| | | 26 | | ) |
| | | 27 | | |
| | | 28 | | begin { |
| | 0 | 29 | | $items = [System.Collections.Generic.List[object]]::new() |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | process { |
| | 0 | 33 | | $items.Add($InputObject) |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | end { |
| | 0 | 37 | | if ($null -eq $Context -or $null -eq $Context.Response) { |
| | 0 | 38 | | Write-KrOutsideRouteWarning |
| | | 39 | | return |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | $payload = if ($items.Count -eq 1) { $items[0] } else { $items } |
| | | 43 | | |
| | | 44 | | # Call the C# method directly with arguments |
| | 0 | 45 | | $Context.Response.WriteFormUrlEncodedResponse($payload, $StatusCode) |
| | | 46 | | } |
| | | 47 | | } |