0

I have a script that sends text (names) to a REST api.

I read the names (and other info) from the AD and construct a JSON string and then use Invoke-RestMethod to send the data to the external REST API. They tell med I should send the data in ISO 8859-1 but I only get ? characters for the NLS.

I tried convering the string from UTF to ISO 8859-1 using the value [Text.Encoding]::GetEncoding(28591) to convert the string to ISO 8859-1 but no effect.

Tried changing the header $headers.Add("Content-Type", "application/json; charset=iso-8859-1")

Even tried sending as html encoded (but it gave html encoding in plan text) [System.Net.WebUtility]::HtmlEncode('something ')

Edit:

To get the right text in the json file I have to add the text HMSHåndbøker like this: $handbook1 = "HMSHÃ¥ndbÞker" I guess this is some kind of UTF coding.

`$larray = @{}
$larray.Add("userName",$user.sn)
...
   $data = @(@{"accessGranted"="true"; "applicationType"=$handbook1; "subapplicationId"="2";},@{"accessGranted"="true"; "applicationType"=$handbook1; "subapplicationId"="4";},@{"accessGranted"="true"; "applicationType"=$handbook1; "subapplicationId"="5";})
   $larray.Add("applicationAccess", $data)
#--------------------------------
#  Create json string from array
#--------------------------------
$json = (Convertto-json -Compress -Depth 4 -InputObject @($larray))`

This works if/when I can hardcode a string but if any text from the AD has NLS characters I cannot get them to the correct format.

Any suggestions?

Edit2:

    $url = "https://simployeramw.azure-api.net/ItasAPI-V2/importlogon"
$lheaders = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$lheaders.Add("Content-Type", "application/json")
$lheaders.Add("X-ITAS-PersonIdentifierType", "EmployeeNumber")
$lheaders.Add("X-ITAS-UnitIdType", "DepartmentCode")
$lheaders.Add("Ocp-Apim-Subscription-Key", $CONST_AUTH_SKEY)
$lheaders.Add("Authorization", "Bearer " + $token)
$rc = Invoke-RestMethod $url -Method 'POST' -Headers $lheaders -Body $json
jmase
  • 1
  • 1
  • Per [Microsoft Learn on `Invoke-RESTMethod`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/invoke-restmethod?view=powershell-7.3), PowerShell 7.3 allows for `-ContentType` to be a string that includes a `charset` setting, e.g., `-ContentType "text/plain; charset=iso-8859-1"`; this is not clearly defined for PowerShell 5.1, but perhaps it's worth trying... – Jeff Zeitlin Jun 29 '23 at 11:32
  • 1
    Can you show the exact code you're using to construct the json string? If you're in contact with the API maintainers I'd ask them for a postman / fiddler / curl example that submits the accented characters correctly - it's entirely possible their API is mangling requests (even the ones that are sent according to their spec). Once you've confirmed it works in postman (or whatever example they give you) you can try to replicate that in PowerShell knowing that any further issues must be in your code rather than their API . – mclayton Jun 29 '23 at 12:10
  • Jeff, I tried that, no change. – jmase Jul 04 '23 at 12:21
  • mclayton, The way I do it with the hardcoded string "HMSHÃ¥ndbÞker" gives the right string in the json ("applicationType":"HMSHåndbøker"). Now if only I know how to do the same with a (not hardcoded) string. – jmase Jul 04 '23 at 12:24
  • Can you also show your call to ```Invoke-RestMethod``` that works when you use the hardcoded string from your edit? It looks like there’s some sort of encoding mismatch for sure… – mclayton Jul 09 '23 at 06:44
  • @jmase the way to address people is to use `@name`, otherwise they won't receive any notifications – phuclv Jul 10 '23 at 01:38
  • @mclayton, have added that code above – jmase Jul 11 '23 at 16:51
  • One thing you can try as a diagnostic test is adding the ```-OutFile``` parameter onto your ```Invoke-RestMethod``` - I’m not 100% sure, but think that will save the *raw* byte stream to disk without trying to unzip it, and you can then inspect the binary file and check whether it really is in gzip format or not with a tool like 7zip or whatever… – mclayton Jul 11 '23 at 17:16

1 Answers1

0

I had a similar problem where I had to add the flag "-Compress" to the ConvertTo-Json command. In order for åäö to be stored correctly. Maybe that can help you?

W_O_L_F
  • 1,049
  • 1
  • 9
  • 16