Skip to content

Commit 8b9544b

Browse files
authored
Adjust PUT method behavior to POST one for default content type in WebCmdlets (PowerShell#19152)
1 parent 8d8f45e commit 8b9544b

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1109,7 +1109,7 @@ internal virtual void FillRequestStream(HttpRequestMessage request)
11091109
{
11101110
WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType] = ContentType;
11111111
}
1112-
else if (request.Method == HttpMethod.Post)
1112+
else if (request.Method == HttpMethod.Post || request.Method == HttpMethod.Put)
11131113
{
11141114
// Win8:545310 Invoke-WebRequest does not properly set MIME type for POST
11151115
WebSession.ContentHeaders.TryGetValue(HttpKnownHeaderNames.ContentType, out string contentType);

test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -759,9 +759,14 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
759759
($result.Output.Content | ConvertFrom-Json).method | Should -Be "TEST"
760760
}
761761

762-
It "Validate Invoke-WebRequest default ContentType for CustomMethod POST" {
763-
$uri = Get-WebListenerUrl -Test 'Post'
764-
$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'"
762+
It "Validate Invoke-WebRequest default ContentType for CustomMethod <method>" -TestCases @(
763+
@{method = "POST"}
764+
@{method = "PUT"}
765+
) {
766+
param($method)
767+
768+
$uri = Get-WebListenerUrl -Test $method
769+
$command = "Invoke-WebRequest -Uri '$uri' -CustomMethod $method -Body 'testparam=testvalue'"
765770
$result = ExecuteWebCommand -command $command
766771
$jsonResult = $result.Output.Content | ConvertFrom-Json
767772
$jsonResult.form.testparam | Should -Be "testvalue"
@@ -2559,9 +2564,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
25592564
$result.Output.method | Should -Be "TEST"
25602565
}
25612566

2562-
It "Validate Invoke-RestMethod default ContentType for CustomMethod POST" {
2563-
$uri = Get-WebListenerUrl -Test 'Post'
2564-
$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod POST -Body 'testparam=testvalue'"
2567+
It "Validate Invoke-RestMethod default ContentType for CustomMethod <method>" -TestCases @(
2568+
@{method = "POST"}
2569+
@{method = "PUT"}
2570+
) {
2571+
param($method)
2572+
$uri = Get-WebListenerUrl -Test $method
2573+
$command = "Invoke-RestMethod -Uri '$uri' -CustomMethod $method -Body 'testparam=testvalue'"
25652574
$result = ExecuteWebCommand -command $command
25662575
$result.Output.form.testparam | Should -Be "testvalue"
25672576
$result.Output.Headers.'Content-Type' | Should -Be "application/x-www-form-urlencoded"

0 commit comments

Comments
 (0)