Azure / azure-functions-powershell-worker

PowerShell language worker for Azure Functions.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Httptrigger] Add support for nullable types in query string

Francisco-Gamino opened this issue · comments

Currently, for an httptrigger function, null or empty values cannot be passed in the query string.

Repro: create a default http trigger function. The name of my function is ProcessData. In the run.ps1 file, define the following:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

Write-Host "PowerShell HTTP trigger function processed a request."

# Update req_query with the query of the request
$req_query = @{
    name = $Request.query.name
    cost = $Request.query.cost
}

Write-Host $req_query

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $req_query
})

The $uri below has a null value for name in the query string.

$uri = 'http://localhost:2056/api/ProcessData?name&cost=10'
$result = irm -Uri $request -Method Post

However, the $Request.query.name property will not be available. By default, the Functions Host, drops any values that are null or empty in the query string. To support this functionality, we need to add the UseNullableValueDictionaryForHttp capability to the PowerShell language worker, so the Functions Host sets the values in the query string as a nullable type. After enabling the capability, we need to add code to the PowerShell language worker to parse the rpcHttp.NullableQuery and headers. This will be a breaking change, so we will not fix this until we enable support for PowerShell 7.4.

As a workaround, the user can use the $TriggerMetadata[$keyName] to retrieve the query property. This is what the function will look like with the workaround which works as expected.

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

Write-Host "PowerShell HTTP trigger function processed a request."

# Update req_query with the query of the request
$req_query = @{
   name = $TriggerMetadata["name"]
   cost = $TriggerMetadata["cost"]
}

Write-Host $req_query

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name response -Value ([HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body = $req_query
})

We should consider fixing this for PowerShell 7.4 release.