PowerShell Azure HTTPTrigger Function Unexpected character encountered

I’ve just built another new PowerShell Azure HTTPTrigger Function locally using VSCode. I tested the Function locally and it did exactly what it was supposed too. I then deployed the Function to Azure and tested it using PowerShell, and it also worked as expected.
Great, deploy the Function and hook it into the workflow. Job done. Wrong, it failed. What the heck?
I tested my Function locally again and success. I tested the deployed Function again using PowerShell and it also works.
Looking into the Azure Logs I find, PowerShell Azure HTTPTrigger Function – Unexpected character encountered when triggered by the workflow. Adding a ‘print’ statement for the input object when called as part of my greater workflow, I could see that the input object has been automatically been converted from JSON to a PowerShell hashtable.

INFORMATION: System.Collections.Hashtable
INFORMATION: Conversion from JSON failed with error: Unexpected character encountered while parsing value: S. Path '', line 0, position 0.
Conversion from JSON failed with error: Unexpected character encountered while parsing value
Conversion from JSON failed with error: Unexpected character encountered while parsing value

But, why is it a hashTable when it was sent a JSON payload? When I tested it locally and against the deployed Function using Invoke-RestMethod, in my Function code I needed to convert the input object from JSON, as I expected I would have to. At first I thought it was this issue again. That particular issue an ex-colleague of mine reported a year back, whereby local testing vs a deployed function exhibited this behaviour. But that has been fixed, and I was able to successfully test my function locally and deployed using Invoke-RestMethod.

Some more investigation and it actually appears to be covered in this issue. It’s how the Azure Function is being triggered. My Azure Function is being triggered by another cloud service which doesn’t allow for any configuration options. Simply the Function URL and the payload to be sent (JSON).

So, how can I mimic the HTTP Trigger myself in order to test my Azure Function as it will be implemented? All you need to do is specify the Content-Type. Often overlooked as PowerShell makes it easy for us, but with Azure PowerShell Functions you need to mimic what other services will also be sending.

Mimicking the Real World

The example below is using Invoke-RestMethod to trigger my Azure PowerShell Function (with the Azure Function URL in the $functionURL variable) and send the JSON input payload, with application/json specified in the headers.
NOTE: I also had to update my Azure PowerShell Function to not attempt to convert the input payload from JSON.

Invoke-RestMethod -Method Post `
    -Uri $functionURL `
    -body $inputJSON `
    -Headers @{'Content-Type' = 'application/json' }

With the headers setting the Content-Type to application/json testing my deployed Azure Function is successful.

PowerShell Azure HTTPTrigger Function Successfully ran
PowerShell Azure HTTPTrigger Function Successfully ran

Removing the Content-Type from the headers shows that the deployed function fails.

PowerShell Azure HTTPTrigger Function Failed Unexpected character encountered
PowerShell Azure HTTPTrigger Function Failed Unexpected character encountered

Summary

If you get a “Conversion from JSON failed with error: Unexpected character encountered while parsing value” error from an Azure PowerShell Function, it is because the Azure PowerShell Function has automatically converted the JSON input object to a PowerShell hashtable.

When developing new Azure PowerShell Functions with JSON input objects, ensure that you test them by setting the Content-Type to application/json. Your Azure PowerShell Function will then automatically convert the input JSON to a hashtable and everything will work as expected.