Incorrect encoding in PowerShell Invoke-WebRequest POST

I use the POST method of the Invoke-WebRequest method to send text data. After sending the text with the wrong encoding.

Script:

$postData = "žluťoučký kůň úpěl ďábelské ódy" Invoke-WebRequest -Uri 'http://www.mydomain.com/' -Method Post -Body $postData -ContentType "text/plain; charset=utf-8" 

Violinist:

 POST http://www.mydomain.com/ HTTP/1.1 User-Agent: Mozilla/5.0 (Windows NT; Windows NT 6.2; cs-CZ) WindowsPowerShell/4.0 Content-Type: text/plain; charset=utf-8 Host: www.mydomain.com Content-Length: 31 zlutouck  kun  pel d belsk   dy 

Edited

It seems like you need to convert the text to utf8 first. PowerShell ISE uses a different encoding by default. In my case, windows-1250.

 $text = "žluťoučký kůň úpěl ďábelské ódy" $postData = [System.Text.Encoding]::UTF8.GetBytes($text) Invoke-WebRequest -Uri 'http://www.mydomain.com/' -Method Post -Body $postData -ContentType "text/plain; charset=utf-8" 
+12
source share
2 answers

This worked for me:

 $postData = "žluťoučký kůň úpěl ďábelské ódy" Invoke-WebRequest -Uri 'http://www.example.com/' -Method Post -Body $postData -ContentType "text/plain; charset=utf-8" 

Adding charset = utf-8 fixed my problem when acute accent characters were converted to special characters.

+5
source share

I had to do two things to solve the problem. Encode the body and add the encoding to the header:

 $body = [System.Text.Encoding]::UTF8.GetBytes($body); $headers = @{ "Content-Type"="application/json; charset=utf-8"; "OData-MaxVersion"="4.0"; "OData-Version"="4.0"; }; Invoke-WebRequest -Uri "$($odataEndpoint)systemusers($userid)" -Method PATCH -Headers $headers -Body $body -UseDefaultCredentials 
+2
source share

All Articles