Powershell: Headers of WebResponseObject object are returned as Array of String instead of String

Created on 7 Aug 2020  路  3Comments  路  Source: PowerShell/PowerShell

Problem description

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.

Steps to reproduce

$r = Invoke-WebRequest -Uri "https://www.microsoft.com/en-us/"
$r.Headers['Content-Type'].GetType()

Expected behavior

On Powershell 5.1:

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object

Actual behavior

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String[]                                 System.Array

Environment data

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
Issue-Question Resolution-Answered

Most helpful comment

This is a breaking change that was made in PowerShell 6.

  • Breaking Changes for PowerShell 6.x
    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

All 3 comments

This is a breaking change that was made in PowerShell 6.

  • Breaking Changes for PowerShell 6.x
    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

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?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

JohnLBevan picture JohnLBevan  路  3Comments

rkeithhill picture rkeithhill  路  3Comments

rudolfvesely picture rudolfvesely  路  3Comments

manofspirit picture manofspirit  路  3Comments

aragula12 picture aragula12  路  3Comments