Documentation for Powershell's Invoke-RestMethod does not mention any parameter which would configure maximum allowed size of request/response headers - Official Documentation.
I'm trying to find out if there are any hidden limitations.
There are planned changes in an API that we are consuming using Powershell commands that will commence including several large (up to 4KB) headers in their responses. There's no easy way for me to mock this kind of response to test whether or not Invoke-RestMethod would complain about such responses.
So far I've investigated official Powershell documentation looking for a parameter that would be related to header size limits. Also scoured internet for similar questions, but no luck.
Emperically from testing, the limit is 65536 bytes...
If you run this quick and dirty powershell-based web server in one internactive console:
$listener = new-object System.Net.Sockets.TcpListener 9999
$listener.Start()
while( $true )
{
    $client = $listener.AcceptTcpClient()
    $endpoint = $client.client.RemoteEndPoint
    $data = ""
    $stream = $client.GetStream()
    $buffer = new-object byte[] 1024
    while (
        $client.Connected -and $stream.DataAvailable -and
        ($i = $stream.Read($buffer, 0, $buffer.Length)) -ne 0)
    {
        $encodedText = New-Object System.Text.ASCIIEncoding
        $data += $encodedText.GetString($buffer, 0, $i)
    }
    write-host "request"
    write-host "-------"
    write-host "$data"
    write-host "--------"
    # Prepare a hard-coded HTTP response
    $responseBody = "Hello from PowerShell!"
    $responseHeaders = [ordered] @{
        "Content-Type"   = "text/plain"
        "Content-Length" = [System.Text.Encoding]::ASCII.GetByteCount($responseBody)
    }
    1..1000 | foreach-object { $responseHeaders.Add("X-Header-$_", "*" *1024) }
    $response = @"
HTTP/1.1 200 OK
$(($responseHeaders.GetEnumerator() | foreach-object { "$($_.Key): $($_.Value)" }) -join "`r`n")
$responseBody
"@
    write-host "response"
    write-host "--------"
    write-host "$response"
    write-host "--------"
    # Send the response
    $responseBytes = [System.Text.Encoding]::ASCII.GetBytes($response)
    $stream.Write($responseBytes, 0, $responseBytes.Length)
    $stream.Flush()
    $stream.Close()
    $client.Close()
}
and then send a request to it from another interactive powershell console:
 invoke-restmethod "http://localhost:9999"
you'll get an error saying this:
Invoke-WebRequest: The HTTP response headers length exceeded the set limit of 65536 bytes.
(If you want to check the powershell webserver code works, change the 1..1000 to 1..10 and Invoke-RestMethod will receive a valid response from it.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With