You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Investigate possible BUG Invoke-WebRequest content type charset #19156
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 WebSessionWebSession.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.
Source-code link (which, however, suggests that this is not the case, if I read it correctly; it suggests that -ContentType is consulted first):
No you are wrong, the code you linked checks for charset in ContentType and then uses the default encoding (UTF8) if there's no charset, it never checks the -Headers
rhubarb-geek-nz, without weighing in on what the right resolution is in this particular case, let me make a general point:
It is helpful to point out what the documented behavior is.
It is not helpful to proclaim that the documented behavior is ipso facto the correct one.
Whether the current behavior ends up being deemed (a) a bug or (b) requiring improvement or (c) neither is a separate matter, which needs to be debated on the merits of the arguments made.
rhubarb-geek-nz, the premise of this issue is that only-ContentType is honored, which is the (only) part covered by the documentation. As such, your comment doesn't address the specific issue being raised: why isn't an - arguably equivalent - charset definition also honored as part of the -Headers argument?
It's fine to say, "here's what the docs say"; it's not helpful to say, "it's not a bug, it's a feature", without addressing the arguments made that describe the current behavior as deficient.
rather than rely on the headers being interpreted.
That part is your conjecture.
And even if this was what the docs meant to say, at the very least the docs should be amended to state that explicitly, especially since it is reasonable to conceive of -ContentType as a shortcut to a Content-Type attribute in a hashtable passed to -Headers.
Of course, a different resolution could be to implement - and document - precedence rules.
Again: I'm not weighing in on what the correct resolution is; I'm trying to point what is required for a constructive, nuanced debate (which shouldn't require several comments just to draw out what your original comment was meant to express).
Given that UTF-8 is JSON's default, and that PowerShell now uses UTF-8 encoding by the default for a [string]-based -Body argument with content type application/json, the following works as expected:
# NO output, because the effectively ISO-8859-1-encoded body cannot be decoded *as UTF8* by https://httpbin.org/post
(Invoke-RestMethod https://httpbin.org/post -Body '{ "foo": "sß" }'-Method POST -ContentType 'application/json; charset=iso-8859-1').json
Trying to make the same intentional mistake with an equivalent Content-Type header field does NOT provoke the symptom, which implies that the charset attribute was ignored by PowerShell, and that UTF-8 was sent:
# !! Output: [pscustomobject] @{ foo = 'sß' } - despite the attempt to send broken encoding# !! PowerShell *ignored* the 'charset' attribute.
(Invoke-RestMethod https://httpbin.org/post -Body '{ "foo": "sß" }'-Method POST -Headers @{
'Content-Type'='application/json; charset=iso-8859-1'
}).json
Activity
iSazonov commentedon Feb 15, 2023
CarloToso Please make more clear repro steps and description.
mklement0 commentedon Feb 17, 2023
If I understand correctly, the problem is that a
charsetvalue is only honored if provided via-ContentType, not also as part of-Headers.Source-code link:
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/WebRequestPSCmdlet.Common.cs
Lines 1491 to 1503 in 27313be
CarloToso commentedon Feb 17, 2023
Michael Klement (@mklement0) Exactly, but I don't know how to test it.
No you are wrong, the code you linked checks for
charsetinContentTypeand then uses the default encoding (UTF8) if there's no charset, it never checks the-Headersmklement0 commentedon Feb 17, 2023
CarloToso, you're right - I misread the source code; I've edited my previous comment to remove the mistaken interpretation.
mklement0 commentedon Feb 20, 2023
rhubarb-geek-nz, without weighing in on what the right resolution is in this particular case, let me make a general point:
It is helpful to point out what the documented behavior is.
It is not helpful to proclaim that the documented behavior is ipso facto the correct one.
Whether the current behavior ends up being deemed (a) a bug or (b) requiring improvement or (c) neither is a separate matter, which needs to be debated on the merits of the arguments made.
mklement0 commentedon Feb 20, 2023
rhubarb-geek-nz, the premise of this issue is that only
-ContentTypeis honored, which is the (only) part covered by the documentation. As such, your comment doesn't address the specific issue being raised: why isn't an - arguably equivalent -charsetdefinition also honored as part of the-Headersargument?It's fine to say, "here's what the docs say"; it's not helpful to say, "it's not a bug, it's a feature", without addressing the arguments made that describe the current behavior as deficient.
mklement0 commentedon Feb 20, 2023
That part is your conjecture.
And even if this was what the docs meant to say, at the very least the docs should be amended to state that explicitly, especially since it is reasonable to conceive of
-ContentTypeas a shortcut to aContent-Typeattribute in a hashtable passed to-Headers.Of course, a different resolution could be to implement - and document - precedence rules.
Again: I'm not weighing in on what the correct resolution is; I'm trying to point what is required for a constructive, nuanced debate (which shouldn't require several comments just to draw out what your original comment was meant to express).
CarloToso commentedon Feb 26, 2023
rhubarb-geek-nz what version of powershell are you using for your tests?
CarloToso commentedon Feb 26, 2023
The default encoding for Invoke-WebRequest in 7.3 is iso-8859-1, changed to utf-8 in 7.4 preview1
CarloToso commentedon Feb 26, 2023
There is also a special workaround (that can maybe be removed), that sets the encoding to utf8 when the body is json
PowerShell/src/Microsoft.PowerShell.Commands.Utility/commands/utility/WebCmdlet/Common/BasicHtmlWebResponseObject.Common.cs
Lines 162 to 182 in fe5f43c
CarloToso commentedon Feb 26, 2023
Maybe you could open a new issue to track that, and ask for feedback there
mklement0 commentedon Feb 26, 2023
CarloToso,
Based on my summary of what I think the issue is, and your confirmation of it as correct, I'm confused by the following sentence in the initial post:
To reconfirm: It should not be ignored, and should instead be honored, just like it already is for
-ContentType, correct?Proceeding on the assumption that my summary is correct:
A simple, setup-free way to verify that
-ContentTypeis honored, but an equivalentContent-Typeheader field is not is via https://httpbin.org/post, which simply echoes the body back to you:[string]-based-Bodyargument with content typeapplication/json, the following works as expected:charsetattribute in the-ContentTypeargument, the.jsonproperty value is empty (the attribute is ignored by https://httpbin.org/post, in line with theapplication/jsonmedia-type definition):Content-Typeheader field does NOT provoke the symptom, which implies that thecharsetattribute was ignored by PowerShell, and that UTF-8 was sent:CarloToso commentedon Feb 26, 2023
Michael Klement (@mklement0) Yes exactly, I'll edit the initial post to make it clearer