| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Writes a response with the specified input object and HTTP status code. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | This function is a wrapper around the Kestrun server response methods. |
| | | 6 | | The response format based on the Accept header or defaults to text/plain. |
| | | 7 | | Content type is determined automatically. |
| | | 8 | | .PARAMETER InputObject |
| | | 9 | | The input object to write to the response body. |
| | | 10 | | .PARAMETER StatusCode |
| | | 11 | | The HTTP status code to set for the response. Defaults to 200 (OK). |
| | | 12 | | |
| | | 13 | | .EXAMPLE |
| | | 14 | | Write-KrResponse -InputObject $myObject -StatusCode 200 |
| | | 15 | | Writes the $myObject to the response with a 200 status code. The content type |
| | | 16 | | is determined automatically based on the Accept header or defaults to text/plain. |
| | | 17 | | .NOTES |
| | | 18 | | This function is designed to be used in the context of a Kestrun server response. |
| | | 19 | | #> |
| | | 20 | | function Write-KrResponse { |
| | | 21 | | [KestrunRuntimeApi('Route')] |
| | | 22 | | [CmdletBinding()] |
| | | 23 | | param( |
| | | 24 | | [Parameter(Mandatory = $true)] |
| | | 25 | | [object]$InputObject, |
| | | 26 | | [Parameter()] |
| | | 27 | | [int]$StatusCode = 200 |
| | | 28 | | ) |
| | | 29 | | # Only works inside a route script block where $Context is available |
| | 0 | 30 | | if ($null -ne $Context.Response) { |
| | | 31 | | # Call the C# method on the $Context.Response object |
| | 0 | 32 | | $Context.Response.WriteResponse($InputObject, $StatusCode) |
| | | 33 | | } else { |
| | 0 | 34 | | Write-KrOutsideRouteWarning |
| | | 35 | | } |
| | | 36 | | } |
| | | 37 | | |