Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1488,14 +1488,16 @@ internal void SetRequestContent(HttpRequestMessage request, string content)
ArgumentNullException.ThrowIfNull(content);

Encoding encoding = null;
if (ContentType is not null)
string contentType = WebSession.ContentHeaders[HttpKnownHeaderNames.ContentType];

if (contentType is not null)
{
// If Content-Type contains the encoding format (as CharSet), use this encoding format
// to encode the Body of the WebRequest sent to the server. Default Encoding format
// would be used if Charset is not supplied in the Content-Type property.
try
{
MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(ContentType);
MediaTypeHeaderValue mediaTypeHeaderValue = MediaTypeHeaderValue.Parse(contentType);
if (!string.IsNullOrEmpty(mediaTypeHeaderValue.CharSet))
{
encoding = Encoding.GetEncoding(mediaTypeHeaderValue.CharSet);
Expand All @@ -1506,7 +1508,7 @@ internal void SetRequestContent(HttpRequestMessage request, string content)
if (!SkipHeaderValidation)
{
ValidationMetadataException outerEx = new(WebCmdletStrings.ContentTypeException, ex);
ErrorRecord er = new(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, ContentType);
ErrorRecord er = new(outerEx, "WebCmdletContentTypeException", ErrorCategory.InvalidArgument, contentType);
ThrowTerminatingError(er);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,13 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$Result.Output.Content | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
}

It "Invoke-WebRequest -ContentType overwrites Content-Type from -Headers." {
$uri = Get-WebListenerUrl -Test 'POST'
$command = "Invoke-WebRequest -Uri '$uri' -ContentType 'application/json' -Headers @{'Content-Type'='plain/text'} -Method 'POST'"
$result = ExecuteWebCommand -command $command
$result.Output.BaseResponse.RequestMessage.Content.Headers.ContentType.MediaType | Should -BeExactly 'application/json'
}

It "Invoke-WebRequest supports sending request as UTF-8." {
$uri = Get-WebListenerUrl -Test 'POST'
# Body must contain non-ASCII characters
Expand All @@ -576,7 +583,7 @@ Describe "Invoke-WebRequest tests" -Tags "Feature", "RequireAdminOnWindows" {
$result = ExecuteWebCommand -command $command
ValidateResponse -response $result

$Result.Output.Encoding.BodyName | Should -BeExactly 'utf-8'
$result.Output.Encoding.BodyName | Should -BeExactly 'utf-8'
$object = $Result.Output.Content | ConvertFrom-Json
$object.Data | Should -BeExactly 'проверка'
}
Expand Down Expand Up @@ -2338,6 +2345,13 @@ Describe "Invoke-RestMethod tests" -Tags "Feature", "RequireAdminOnWindows" {
$Result.Output | Should -Match '⡌⠁⠧⠑ ⠼⠁⠒ ⡍⠜⠇⠑⠹⠰⠎ ⡣⠕⠌'
}

It "Invoke-RestMethod -ContentType overwrites Content-Type from -Headers." {
$uri = Get-WebListenerUrl -Test 'POST'
$command = "Invoke-RestMethod -Uri '$uri' -ContentType 'application/json' -Headers @{'Content-Type'='plain/text'} -Method 'POST'"
$result = ExecuteWebCommand -command $command
$result.Output.headers."Content-Type" | Should -BeExactly 'application/json'
}

Comment thread
CarloToso marked this conversation as resolved.
It "Invoke-RestMethod supports sending requests as UTF8" {
$uri = Get-WebListenerUrl -Test POST
# Body must contain non-ASCII characters
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.