The headers of Microsoft.PowerShell.Commands.BasicHtmlWebResponseObject object returned by Invoke-WebRequest, are returned as Arrays of String instead of String.
This problem seems specific to the Headers dictionary of the WebResponse object.
$r = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/"
$r.Headers['Content-Type'].GetType()
On Powershell 5.1:
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String[] System.Array
Name Value
---- -----
PSVersion 7.0.3
PSEdition Core
GitCommitId 7.0.3
OS Linux 3.10.0-1127.10.1.el7.x86_64 #1 SMP Tue May 26 15:05:43 EDT 2020
Platform Unix
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0鈥
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0
This is a breaking change that was made in PowerShell 6.
This issue has been marked as answered and has not had any activity for 1 day. It has been closed for housekeeping purposes.
Thank you @mkht ; not that I understand why this would be an improvement over the old format, but at least it explains the behavior. It cost me over a day to find out why a script that talks to an API did work on PS 5.1, but failed on PS 7.0.
The script authenticates to the API, which returns a token in a header. When using the returned array of string to set the value of a token header for subsequent calls to the API (together with a session variable), Invoke-WebRequest apparently did not convert the headers to a normal string, and the API returned HTTP 401/Unauthorized. I had to convert the token into a normal string manually using -join before the API accepted the token header. Is this a shortcoming of the API or should Invoke-WebRequest convert the array of strings before sending it to the server?
In other words, I really don't have a clue why having an array of characters (because that is what is returned now) over a normal string, is a benefit. Can someone elaborate on this?
Most helpful comment
This is a breaking change that was made in PowerShell 6.
https://docs.microsoft.com/en-us/powershell/scripting/whats-new/breaking-changes-ps6?view=powershell-6#changes-to-web-cmdlets
> BasicHtmlWebResponseObject.Headers values are now String[] instead of String