Fix Request Charset Issues in Web Cmdlets#8742
Conversation
|
The codefactor error is nowhere near the code I touched. *shrugs |
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Outdated
Show resolved
Hide resolved
test/powershell/Modules/Microsoft.PowerShell.Utility/WebCmdlets.Tests.ps1
Show resolved
Hide resolved
| try | ||
| { | ||
| var mediaTypeHeaderValue = new MediaTypeHeaderValue(ContentType); | ||
| var mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType); |
There was a problem hiding this comment.
We could use MediaTypeHeaderValue.TryParse() to avoid exceptions.
There was a problem hiding this comment.
No need. we already have logic in place to handle the exceptions and perform the correct operations.
There was a problem hiding this comment.
We should avoid exceptions if we can. If the exceptions come only from the Parse() method we should use TryParse().
There was a problem hiding this comment.
Except that we have different behavior depending on the type of exception. We would have to build our own logic to figure that out. When the type of exception determines behavior it is perfectly acceptable to use them in a try/catch.
There was a problem hiding this comment.
My comment is not blocking.
|
@markekraus Thanks for fixing the issue! |
PR Summary
Closes #7618
Instantiating a new
MediaTypeHeaderValueobject fails when the-ContentTypeparameter includes a charset such asapplication/json; charset=utf-8. This makes it impossible to set the content encoding on web requests. Moving toParse()ensures we actually get a properMediaTypeHeaderValuewhen the charset is present, thus allowing users to set their request encoding via proper-ContentTypevalues.PR Context
#7618
#8736
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright headerWIP:or[ WIP ]to the beginning of the title (theWIPbot will keep its status check atPendingwhile the prefix is present) and remove the prefix when the PR is ready.[feature]to your commit messages if the change is significant or affects feature tests