Skip to content

Invoke-WebRequest and Invoke-RestMethod in Powershell 7.4.2 - errors using PUT method #23827

@bondido

Description

@bondido

Prerequisites

Steps to reproduce

I'm receiving error calling WebCmdlets (Invoke-WebRequest, Invoke-RestMethod) using PUT method if I don't explicitly set content-type header.

Calling
Invoke-WebRequest -Method PUT -Uri $someUri -Body $someBody
Returns Error in PowerShell 7.4.2:
Invoke-WebRequest: The given key 'Content-Type' was not present in the dictionary.

(the error is thrown before any network call is even started)

The exact same call runs OK with PowerShell 7.4.1 and all lower versions that I was able to test.

Running:
Invoke-WebRequest -Method PUT -Uri $someUri -Body $someBody -ContentType 'application/x-www-form-urlencoded'
also works OK - even with powershell 7.4.2.

I see the change in v7.4.2 release notes #21049
But, as I understand it, setting content-type (by request headers collection or -ContentType parameter) isn't required and omitting it shouldn't be any issue regardless of pwsh version. Content-type can be default ('application/x-www-form-urlencoded') or none depending on pwsh version.

According to the available docs:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.4#-contenttype

If this parameter is omitted and the request method is POST, Invoke-RestMethod sets the content type to application/x-www-form-urlencoded. Otherwise, the content type isn't specified in the call.

I could successfully call PUT requests without explicitly specifying Content-type just until pwsh 7.4.2.

Expected behavior

Invoke-WebRequest -Method PUT -Uri "http://fake" -Body "fake"
Invoke-WebRequest: Resource temporarily unavailable

Actual behavior

> Invoke-WebRequest -Method PUT -Uri "http://fake" -Body "fake"
Invoke-WebRequest: The given key 'Content-Type' was not present in the dictionary.

Error details

Invoke-WebRequest: The given key 'Content-Type' was not present in the dictionary.

Environment data

Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Ubuntu 22.04.4 LTS
Platform                       Unix
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    In-PRIndicates that a PR is out for the issueIssue-BugIssue has been identified as a bug in the productUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdlets-Utilitycmdlets in the Microsoft.PowerShell.Utility module

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions