Powershell: Wrong encode in Invoke-RestMethod -Body (problem with cyrilic symbols)

Created on 23 Aug 2018  Â·  3Comments  Â·  Source: PowerShell/PowerShell

Steps to reproduce

Invoke-RestMethod -Uri $URI -Method Put -ContentType 'application/json' -Body '{"jsonrpc":"2.0","method":"get.object","params":{"latname":"testing","cyrname":"проверка"}}'

Expected behavior

http request json

{"jsonrpc":"2.0","method":"get.object","params":{"latname":"testing","cyrname":"проверка"}}

Actual behavior

http request json

{"jsonrpc":"2.0","method":"get.object","params":{"latname":"testing","cyrname":"????????"}}

Environment data

Name Value
---- -----
PSVersion 6.1.0-rc.1
PSEdition Core
GitCommitId 6.1.0-rc.1
OS Microsoft Windows 10.0.17134
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

Description

It works good if -InFile Parameter is used

Invoke-RestMethod -Uri $URI -Method Put -ContentType 'application/json' -InFile $FilePath 

File must be encoded in UTF-8 with no BOM

I think cmdlet encodes body to ASCII
In PS 5.1, 6.0 the same bug is.
Thanks

Area-Cmdlets-Utility Issue-Question Resolution-Fixed

Most helpful comment

I can repro this with using $uri = 'https://httpbin.org/put'

I will have to look for other open issues on this later, but I believe this is a duplicate.

The underlying cause is dotnet/corefx#16290 . We rely on MediaTypeHeaderValue to determine what charset encoding to use but MediaTypeHeaderValue dose snot properly parse charset from a supplied content type. The solution, if MediaTypeHeaderValue worked properly, would be to supply -ContentType 'application/json; charset=UTF-8'. This works in Windows PowerShell 5.1, but not in 6+.

I have suggested before we may want to add a -BodyEncoding parameter to allow for specifically setting this. However, we may just need to work around MediaTypeHeaderValue's limitation with some string parsing on -ContentType

All 3 comments

@markekraus Could you please comment?

I can repro this with using $uri = 'https://httpbin.org/put'

I will have to look for other open issues on this later, but I believe this is a duplicate.

The underlying cause is dotnet/corefx#16290 . We rely on MediaTypeHeaderValue to determine what charset encoding to use but MediaTypeHeaderValue dose snot properly parse charset from a supplied content type. The solution, if MediaTypeHeaderValue worked properly, would be to supply -ContentType 'application/json; charset=UTF-8'. This works in Windows PowerShell 5.1, but not in 6+.

I have suggested before we may want to add a -BodyEncoding parameter to allow for specifically setting this. However, we may just need to work around MediaTypeHeaderValue's limitation with some string parsing on -ContentType

Example from product environment, used ZabbixAPI

Login Request

$URI = 'https://zbx.contoso.com/zabbix/api_jsonrpc.php'
$JsonLoginBody = '{"jsonrpc":"2.0","method":"user.login","params":{"user":"testuser","password":"testPss"},"id":"1"}'
Invoke-RestMethod -Uri $URI -Method Put -Body $JsonLoginBody -ContentType 'application/json'

http request json

{"jsonrpc":"2.0","method":"user.login","params":{"user":"testuser","password":"testPss"},"id":"1"}

Answer

jsonrpc result                           id
------- ------                           --
2.0     29ff8c488fea131036e331b9731d4ae2 1

Search item request

$JsonSearchRequest = '{"jsonrpc":"2.0","method":"item.get","params":{"hostids":"20321","search":{"name":"вход"}},"auth":"29ff8c488fea131036e331b9731d4ae2","id":"1"}'
Invoke-RestMethod -Uri $URI -Method Put -Body $JsonSearchRequest -ContentType 'application/json'

http request json

{"jsonrpc":"2.0","method":"item.get","params":{"hostids":"20321","search":{"name":"????"}},"auth":"29ff8c488fea131036e331b9731d4ae2","id":"1"}

Answer

jsonrpc result id
------- ------ --
2.0     {}     1
Was this page helpful?
0 / 5 - 0 ratings