Special Characters utf8-bom with the API

Solved
bjowol
New here

Special Characters utf8-bom with the API

after updating all the organization name with PowerShell and the API I noticed a formatting error.
Special Characters like øæå are replaced with squares.

 

Does anyone know what kind of little parameter i need to add in my code to make it support our nordic characters?

any help would be greatly appreciated.

1 Accepted Solution
BrechtSchamp
Kind of a big deal

It was this that finally did the trick I suppose, in combination with the correct charset setting?

 

$body = [System.Text.Encoding]::UTF8.GetBytes($json)

 

View solution in original post

9 Replies 9
BrechtSchamp
Kind of a big deal

Have you tried using URL encoding for those characters? The name parameter ends up as a param in the get URL so depending on how the script works this might be needed.

øæå = %C3%B8%C3%A6%C3%A5

 

Online converter if needed:

https://www.w3schools.com/tags/ref_urlencode.asp

bjowol
New here

$customuri = $org.samlConsumerUrl.Split('/')[2].split('.')[0]
$updateuri = 'https://{1}.meraki.com/api/v0/organizations/{0}' -f $org.id,$customuri
$parms = @{
name = $WantedName
}
$json = $parms | ConvertTo-Json
$change = Invoke-RestMethod -Method Put -Uri $updateuri -Body $json -Headers $header
$change

2019-02-01 09_01_48-● Update.Meraki.Org.Name.ps1 - Untitled (Workspace) - Visual Studio Code.png2019-02-01 09_02_38-Organization settings - Meraki Dashboard.png
 
Unfortunatly this did not work.
BrechtSchamp
Kind of a big deal

Hmm okay. Try changing this:

"Content-Type" = 'application/json'

To this:

"Content-Type" = 'application/json ; charset=utf-8'

 

bjowol
New here

thanks for your input.

this is my current header:

$header = @{
    'X-Cisco-Meraki-API-Key' = $apikey
    'Content-Type' = 'application/json ; charset=utf-8'
    'Accept-Language' = 'nb-NO,nb;q=0.9,no-NO;q=0.8,no;q=0.6,nn-NO;q=0.5,nn;q=0.4,en-US;q=0.3,en;q=0.1'
    
}

Ive also tried a few other charsets, like iso and sorts.

 

also tried this.

$header = @{
    'X-Cisco-Meraki-API-Key' = $apikey
    'Content-Type' = 'application/json'
    'Accept-Language' = 'nb-NO,nb;q=0.9,no-NO;q=0.8,no;q=0.6,nn-NO;q=0.5,nn;q=0.4,en-US;q=0.3,en;q=0.1'
    'Accept-Charset'=  'utf-8, iso-8859-1;q=0.5, *;q=0.1'
}
BrechtSchamp
Kind of a big deal

This worked for me:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

$api_key = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'

$header = @{
	
	"X-Cisco-Meraki-API-Key" = $api_key
	"Content-Type" = 'application/json ; charset=utf-8'
	
}

$api = @{

	"endpoint" = 'https://xxxx.meraki.com/api/v0'

}

$api.url = '/networks/xxxmy-network-idxxxx'
$uri = $api.endpoint + $api.url

$parms = @{
	name = 'My Network øæå'
}
$json = $parms | ConvertTo-Json
$change = Invoke-RestMethod -Method Put -Uri $uri -Body $json -Headers $header
$change

I set the encoding of the file to UTF-8 from Notepad++ and then ran it from powershell.

 

Maybe what Nick did here will also help:

 

system_encoding.PNG

Source: https://stackoverflow.com/questions/21598398/wrong-encoding-on-powershell-invoke-webrequest-post

bjowol
New here

i appreciate your responses. i tried them in both vscode(with all types of encoding) and in ISE.

 

i also tried this

            $change = Invoke-RestMethod -Method Put -Uri $updateuri -Body $json -Headers $header -ContentType "text/plain; charset=utf-8"
            $change

I have a support case open with meraki. ill see if they can come up with something.

 

i have not tried to do the same with a network name. cant think of any reasons why there would be a difference between network name and org name, but there might be.

bjowol
New here

found a solution based on your reply.

 

$header = @{
     "X-Cisco-Meraki-API-Key" = $apikey 
     "Content-Type" = 'application/json ; charset=utf-16' 
}

            $customuri = $org.samlConsumerUrl.Split('/')[2].split('.')[0]
            $updateuri = 'https://{1}.meraki.com/api/v0/organizations/{0}' -f $org.id,$customuri
            $parms = @{
                name = $WantedName
            }
            $json = $parms | ConvertTo-Json
            $body = [System.Text.Encoding]::UTF8.GetBytes($json)

            $change = Invoke-RestMethod -Method Put -Uri $updateuri -Body $body -Headers $header
            $change
BrechtSchamp
Kind of a big deal

It was this that finally did the trick I suppose, in combination with the correct charset setting?

 

$body = [System.Text.Encoding]::UTF8.GetBytes($json)

 

bjowol
New here

it was probably just that one-liner.

i tested with a bunch of different header parameters and running powershell in different charsets.

 

glad you pointed me to the solution though!


I believe it converts the data to bytes, and just sends the bytes, ignoring charsets or something.

Get notified when there are additional replies to this discussion.