1

I am facing an issue with my Azure function (Powershell, timer trigger on Linux) which extracts values from Power Bi Service and pass them to a DevOps Pipeline when triggering it. The triggering is done through Invoke-RestMethod.

The function works fine until it gets values which contain special german characters (Ü, Ö, Ä,...) then the body of the request is somehow broken and the function fails.

I think the problem is related to the encoding but the only possible solution I found is to add charset=utf-8 to the header. Unfortunately, it did not solve the problem.

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type", "application/json; charset=utf-8")
$headers.Add("Authorization", "Basic $secret")
    
$body = "{`"templateParameters`": {`"items`": `"$items`"}}"

$response = Invoke-RestMethod 'https://dev.azure.com/<repos>/<project>/_apis/pipelines/1/runs?api-version=7.0' -Method 'POST' -Headers $headers -Body $body

Items is a string of the form: [['filename','userx','Workspace','givendateTime']]

when the Workspace has a Ö instead of 'o' an exception is raised:

Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: 
runParameters","typeName":"System.ArgumentNullException, mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
+ ... $response = Invoke-RestMethod 'https://dev.azure.com/...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

Using Postman with the same parameters and values works fine. Write-Host $body just before the Invoke-RestMethod is fine and the characters are correct.

Any help will be much appreciated :)

Daniel Mann
  • 57,011
  • 13
  • 100
  • 120

0 Answers0