< Summary - Kestrun — Combined Coverage

Information
Class: Public.Request.Get-KrRequestHeader
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Request/Get-KrRequestHeader.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 14
Coverable lines: 14
Total lines: 89
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 14:53:17 Line coverage: 0% (0/2) Total lines: 28 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/04/2025 - 18:11:31 Line coverage: 0% (0/2) Total lines: 29 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0510/15/2025 - 01:01:18 Line coverage: 0% (0/14) Total lines: 88 Tag: Kestrun/Kestrun@7c4ce528870211ad6c2d2398c31ec13097fc584012/12/2025 - 17:27:19 Line coverage: 0% (0/14) Total lines: 89 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Request/Get-KrRequestHeader.ps1

#LineLine coverage
 1<#
 2    .SYNOPSIS
 3        Retrieves a request header value from the HTTP request.
 4    .DESCRIPTION
 5        This function accesses the current HTTP request context and retrieves the value
 6        of the specified request header by name.
 7    .PARAMETER Name
 8        The name of the request header to retrieve from the HTTP request.
 9    .PARAMETER AsInt
 10        If specified, converts the header value to an integer.
 11    .PARAMETER AsBool
 12        If specified, converts the header value to a boolean.
 13    .PARAMETER AsDouble
 14        If specified, converts the header value to a double.
 15    .PARAMETER AsString
 16        If specified, converts the header value to a string.
 17    .PARAMETER ThrowIfMissing
 18        If specified, throws an error if the header is not found.
 19        By default, it returns $null if not found.
 20    .EXAMPLE
 21        $value = Get-KrRequestHeader -Name "param1"
 22        Retrieves the value of the request header "param1" from the HTTP request.
 23    .EXAMPLE
 24        $id = Get-KrRequestHeader -Name "id" -AsInt -ThrowIfMissing
 25        Retrieves the value of the header "id" as an integer, throwing an error if it's missing.
 26    .EXAMPLE
 27        $flag = Get-KrRequestHeader -Name "flag" -AsBool
 28        Retrieves the value of the header "flag" as a boolean.
 29    .EXAMPLE
 30        $auth = Get-KrRequestHeader -Name "Authorization" -AsString -ThrowIfMissing
 31        Retrieves the value of the "Authorization" header as a string, throwing an error if it's missing.
 32    .OUTPUTS
 33        Returns the value of the specified request header, or $null if not found.
 34    .NOTES
 35        This function is designed to be used in the context of a Kestrun server response.
 36#>
 37function Get-KrRequestHeader {
 38    [KestrunRuntimeApi('Route')]
 39    [CmdletBinding(DefaultParameterSetName = 'Default')]
 40    [OutputType([string])]
 41    [OutputType([int])]
 42    [OutputType([bool])]
 43    [OutputType([double])]
 44    [OutputType([object])]
 45    param(
 46        [Parameter(Mandatory = $true)]
 47        [string]$Name,
 48
 49        [Parameter(parameterSetName = 'Int')]
 50        [switch]$AsInt,
 51
 52        [Parameter(parameterSetName = 'Bool')]
 53        [Alias('AsBoolean')]
 54        [switch]$AsBool,
 55
 56        [Parameter(parameterSetName = 'Double')]
 57        [switch]$AsDouble,
 58
 59        [Parameter(parameterSetName = 'String')]
 60        [switch]$AsString,
 61
 62        [Parameter()]
 63        [switch]$ThrowIfMissing
 64    )
 065    if ($null -ne $Context -and $null -ne $Context.Request -and $null -ne $Context.Request.Headers) {
 066        if ($ThrowIfMissing -and -not $Context.Request.Headers.ContainsKey($Name)) {
 067            throw [System.ArgumentException]::new("Missing required header: $Name")
 68        }
 69        # Get the request header value from the request
 070        $value = $Context.Request.Headers[$Name]
 071        if ($AsInt) {
 072            return [int]$value
 73        }
 074        if ($AsBool) {
 075            return [bool]$value
 76        }
 077        if ($AsDouble) {
 078            return [double]$value
 79        }
 080        if ($AsString) {
 081            return $value.ToString()
 82        }
 83        # Default: return as-is (string or array of strings)
 084        return $value
 85    } else {
 86        # Outside of route context
 087        Write-KrOutsideRouteWarning
 88    }
 89}

Methods/Properties

Get-KrRequestHeader()