Skip to content

Investigate possible BUG Invoke-WebRequest content type charset #19156

@CarloToso

Description

@CarloToso

Prerequisites

Steps to reproduce

Invoke-WebRequest POST with -Body (string) and -Headers containing a content type with charset (ex. plain/text; charset="windows-1251") and no -ContentType.

Invoke-WebRequest -URI "some uri" -Method POST -Headers @{Content-Type="plain/text; charset=windows-1251"} -Body "string encoded with windows-1251 charset"

The charset in the headers are ignored be ignored and default to UTF8.
This doesn't happen if we use -ContentType

Invoke-WebRequest -URI "some uri" -Method POST -ContentType "plain/text; charset=windows-1251" -Body "string encoded with windows-1251 charset"

Both should have the same behaviour

I found this possible bug while reading the code; I haven't encountered it in the real world so I don't really know how we could test it (and if testing it is even possible)

Expected behavior

SetRequestContent should try to get the charset from the ContentType in Headers saved in the WebSession
WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType]

Actual behavior

SetRequestContent only checks for -ContentType charset

Error details

While writing some code in `WebRequestPSCmdlet.Common.cs` think I found a BUG (line 1491) it should check for `WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType]` instead of `ContentType` (this should be ok because if both are present `ContentType` overwrites `WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType]`) unfortunately I don't know how to test it (I think it would require sending a wierdily encoded string as Body), if confirmed I'll open another PR to fix it.

Environment data

Latest master
https://github.com/PowerShell/PowerShell/blob/60b4e9704fca546f76e1128e30de4077c51a3ca2/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs

Visuals

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions