0
votes

In Azure functions the http request is parsed from a $req JSON object like so

# POST method: $req $requestBody = Get-Content $req -Raw | ConvertFrom-Json

I am finding it very difficult (coming from powershell ISE as a sys admin) to get the exact structure of this variable, for example, this is the default run.ps1 whenever a PowerShell function is created

# POST method: $req
$requestBody = Get-Content $req -Raw | ConvertFrom-Json
$name = $requestBody.name

# GET method: each querystring parameter is its own variable
if ($req_query_name) 
{
    $name = $req_query_name 
}

Out-File -Encoding Ascii -FilePath $res -inputObject "Hello $name"

so if the http method is POST $name = $requestBody.name if it's GET $name = $req_query_name

how to I access the rest of the request such as the http headers, user agent timestamps etc?

for example this is not well documented and would have been nice to know, especially if dealing with a large amount of query params

if ($req_method -eq 'GET'){
something with GET
}
if ($req_method -eq 'POST'){
something with POST
}

this led me on a guessing game, $req_headers? - nope $req_context? nope My question is how can I find out everything about the incoming request? why are variable names $req_something or $req_something_something - what is this structure?

1
You can use Get-Variable to get a list of all variables in the session, and $var | Get-Member to see all properties on a variable.Dhruv Murarka
that is how I would normally inspect variables in the ISE - but in Azure functions console is DOS and in kudu, while there is a powershell console it doesn't seem to be in the runspace e.g. it's only for administering the web service environmentSum1sAdmin
You can save the output of the commands in a variable, as a string ($var | Out-String). Make that part of the function code itself, and return it as the function's response, or log it, or store it to a file, etc.Dhruv Murarka
yes, so the only documented variable is $req for the request body and I want the headers - $req is read (get-content) from a file, outputing the value of $req is not helping - the result for the example given is {"name":"Azure"}Sum1sAdmin

1 Answers

2
votes

You should get the headers as $req_headers_<HeaderName>.

enter image description here

enter image description here