Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -663,8 +663,15 @@ internal virtual void PrepareSession()
{
foreach (string key in Headers.Keys)
{
// add the header value (or overwrite it if already present)
WebSession.Headers[key] = Headers[key].ToString();
var value = Headers[key];

// null is not valid value for header.
// We silently ignore header if value is null.
if (!(value is null))
{
// add the header value (or overwrite it if already present)
WebSession.Headers[key] = value.ToString();
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3476,4 +3476,19 @@ Describe "Web cmdlets tests using the cmdlet's aliases" -Tags "CI", "RequireAdmi
$result = Invoke-RestMethod $uri
$result.Hello | Should -Be "world"
}

It "Web cmdlets ignore headers with null value" {
$query = @{
body = "hello"
contenttype = 'text/plain'
}
$uri = Get-WebListenerUrl -Test 'Response' -Query $query

# Core throws if a header has null value.
# We ignore such headers so no exception is expected.
{ Invoke-WebRequest -Uri $uri -Headers @{ "Location" = $null } } | Should -Not -Throw
{ Invoke-WebRequest -Uri $uri -ContentType $null } | Should -Not -Throw
{ Invoke-RestMethod -Uri $uri -Headers @{ "Location" = $null } } | Should -Not -Throw
{ Invoke-RestMethod -Uri $uri -ContentType $null } | Should -Not -Throw
}
}