< Summary - Kestrun — Combined Coverage

Information
Class: Public.Request.Get-KrRequestBody
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Request/Get-KrRequestBody.ps1
Tag: Kestrun/Kestrun@eeafbe813231ed23417e7b339e170e307b2c86f9
Line coverage
0%
Covered lines: 0
Uncovered lines: 20
Coverable lines: 20
Total lines: 75
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 09/12/2025 - 13:32:05 Line coverage: 0% (0/9) Total lines: 51 Tag: Kestrun/Kestrun@63ea5841fe73fd164406accba17a956e8c08357f12/12/2025 - 17:27:19 Line coverage: 0% (0/12) Total lines: 61 Tag: Kestrun/Kestrun@826bf9dcf9db118c5de4c78a3259bce9549f0dcd02/05/2026 - 00:28:18 Line coverage: 0% (0/20) Total lines: 75 Tag: Kestrun/Kestrun@d9261bd752e45afa789d10bc0c82b7d5724d9589

Coverage delta

Coverage delta 1 -1

Metrics

File(s)

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

#LineLine coverage
 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#>
 22function Get-KrRequestBody {
 23    [KestrunRuntimeApi('Route')]
 24    [CmdletBinding()]
 25    [OutputType([Hashtable])]
 26    param(
 27        [switch]$Raw,
 28        [Type]$Type
 29    )
 30
 031    if ($null -ne $Context.Request) {
 032        $body = $Context.Request.Body
 033        $contentType = $Context.Request.ContentType
 34        # Check if we need to read the stream (e.g., for compressed requests)
 035        $hasEncoding = -not [string]::IsNullOrWhiteSpace($Context.Request.Headers['Content-Encoding'])
 036        $bodyIsString = $body -is [string]
 037        $needsStreamRead = $hasEncoding -and (-not $bodyIsString -or [string]::IsNullOrEmpty($body))
 38        # Need to read the stream if not already read
 039        if ($needsStreamRead) {
 040            $reader = [System.IO.StreamReader]::new($Context.HttpContext.Request.Body, [System.Text.Encoding]::UTF8, $fa
 41            try {
 042                $body = $reader.ReadToEndAsync().GetAwaiter().GetResult()
 43            } finally {
 044                $reader.Dispose()
 45            }
 46        }
 47        # Return the raw body if specified
 048        if ($Raw) {
 49            # Get the raw request body value from the request
 050            return $body
 51        }
 52        # Parse the request body based on the specified type or content type
 053        if ($null -ne $Type) {
 054            return [Kestrun.Utilities.Json.JsonSerializerHelper]::FromJson($body, $Type)
 55        }
 56        # Parse based on Content-Type
 057        switch ($contentType) {
 58            'application/json' {
 059                return $body | ConvertFrom-Json -AsHashtable
 60            }
 61            'application/yaml' {
 062                return [Kestrun.Utilities.YamlHelper]::ToHashtable( $body)
 63            }
 64            'application/x-www-form-urlencoded' {
 065                return $Context.Request.Form
 66            }
 67            'application/xml' {
 068                return [Kestrun.Utilities.XmlHelper]::ToHashtable( $body)
 69            }
 70            default {
 071                return $body
 72            }
 73        }
 74    }
 75}

Methods/Properties

Get-KrRequestBody()