Reputation: 533
I have the code below runs OK in Azure Functions on it's own and displays output. However the code is static, I need the URL (top line) to be able to be passed through as a parameter with an HTTP on-demand trigger.
The 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 and then work with the parameter in the PowerShell code.
# 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
Side note, if you get the error below on your computer…..
New-Object : Cannot find type [System.Net.Http.HttpClient]: verify that the assembly containing this type is loaded
Run this block of code, seems that you need to ‘warm’ up the system in order to find the ‘type’, running Find-Type httpClient a few times seems to wake up the system to realise Yes, it does have this type installed.
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')
Upvotes: 0
Views: 576
Reputation: 12538
The default PowerShell HTTP Trigger template shows an example of that.
Query string parameters will be made available to your script as variables in the format req_query_<parametername>
, so in your example above the URL parameter would be accessible using: $req_query_url
The following function is a simple example of just returning the parameter
Out-File -Encoding Ascii -FilePath $res -inputObject "URL parameter $req_query_url"
Upvotes: 1