将命令式参数传递给 Azure Functions
Passing imperative parameters through to Azure Functions
我让下面的代码在 Azure Functions 中运行正常并显示输出。然而,代码是静态的,我需要 URL(顶行)能够作为 HTTP 按需触发器的参数传递。
article here talks about Binding at runtime via imperative bindings, however it’s not 100% clear as to how to pass an HTTP based parameter e.g. https://myfunction.azurewebsites.net/api/AustereoJustPlaying?url=legacy.scahw.com.au/2classicrock_128.xspf 然后在 PowerShell 代码中使用参数。
# Get the initial metadata for the stream
$url = 'http://legacy.scahw.com.au/2classicrock_128.xspf'
$iwr = Invoke-RestMethod -Uri $url
# Build up the .Net web client
$HttpCompletionOption = 'ResponseContentRead'
$webClient = New-Object System.Net.Http.HttpClient
$webclient.DefaultRequestHeaders.Add('Icy-MetaData', '1')
# Get the Stream URL
$null = $iwr.InnerXml -match '<location>(?<location>.*)<\/location>'
$location = $matches.location
# Fire up the stream
$response = $webClient.GetAsync($location,$HttpCompletionOption)
$null = $webclient.DefaultRequestHeaders.Remove('Icy-MetaData')
# Pause until the stream title actually fires up
Start-Sleep -Seconds 2
# Grab the song
$iwr = Invoke-RestMethod -Uri $url
$null = $iwr.InnerXml -match '<title>(?<song>.*)<\/title>'
# Kill the stream
$webclient.Dispose()
# Output the song
$matches.song
旁注,如果您在计算机上遇到以下错误......
新对象:找不到类型[System.Net.Http.HttpClient]:验证是否已加载包含此类型的程序集
运行 这段代码,似乎你需要“预热”系统才能找到“类型”,运行 Find-Type httpClient 几次似乎被唤醒up系统实现 是的,确实安装了这个类型。
function Find-Type ([regex]$pattern)
{
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() |
Select-Object -ExpandProperty FullName | Select-String $pattern
}
Do {
cls
$TypeSearch = Find-Type httpClient
} until ($TypeSearch -match 'System.Net.Http.HttpClient')
默认的 PowerShell HTTP 触发器模板显示了一个示例。
查询字符串参数将以 req_query_<parametername>
格式的变量形式提供给您的脚本,因此在上面的示例中,可以使用以下方式访问 URL 参数:$req_query_url
下面的函数只是简单的返回参数的例子
Out-File -Encoding Ascii -FilePath $res -inputObject "URL parameter $req_query_url"
我让下面的代码在 Azure Functions 中运行正常并显示输出。然而,代码是静态的,我需要 URL(顶行)能够作为 HTTP 按需触发器的参数传递。
article here talks about Binding at runtime via imperative bindings, however it’s not 100% clear as to how to pass an HTTP based parameter e.g. https://myfunction.azurewebsites.net/api/AustereoJustPlaying?url=legacy.scahw.com.au/2classicrock_128.xspf 然后在 PowerShell 代码中使用参数。
# Get the initial metadata for the stream
$url = 'http://legacy.scahw.com.au/2classicrock_128.xspf'
$iwr = Invoke-RestMethod -Uri $url
# Build up the .Net web client
$HttpCompletionOption = 'ResponseContentRead'
$webClient = New-Object System.Net.Http.HttpClient
$webclient.DefaultRequestHeaders.Add('Icy-MetaData', '1')
# Get the Stream URL
$null = $iwr.InnerXml -match '<location>(?<location>.*)<\/location>'
$location = $matches.location
# Fire up the stream
$response = $webClient.GetAsync($location,$HttpCompletionOption)
$null = $webclient.DefaultRequestHeaders.Remove('Icy-MetaData')
# Pause until the stream title actually fires up
Start-Sleep -Seconds 2
# Grab the song
$iwr = Invoke-RestMethod -Uri $url
$null = $iwr.InnerXml -match '<title>(?<song>.*)<\/title>'
# Kill the stream
$webclient.Dispose()
# Output the song
$matches.song
旁注,如果您在计算机上遇到以下错误......
新对象:找不到类型[System.Net.Http.HttpClient]:验证是否已加载包含此类型的程序集
运行 这段代码,似乎你需要“预热”系统才能找到“类型”,运行 Find-Type httpClient 几次似乎被唤醒up系统实现 是的,确实安装了这个类型。
function Find-Type ([regex]$pattern)
{
[System.AppDomain]::CurrentDomain.GetAssemblies().GetTypes() |
Select-Object -ExpandProperty FullName | Select-String $pattern
}
Do {
cls
$TypeSearch = Find-Type httpClient
} until ($TypeSearch -match 'System.Net.Http.HttpClient')
默认的 PowerShell HTTP 触发器模板显示了一个示例。
查询字符串参数将以 req_query_<parametername>
格式的变量形式提供给您的脚本,因此在上面的示例中,可以使用以下方式访问 URL 参数:$req_query_url
下面的函数只是简单的返回参数的例子
Out-File -Encoding Ascii -FilePath $res -inputObject "URL parameter $req_query_url"