How do I create a HTTP Trigger Azure Function that takes in 2 parameters and outputs the 2 parameters as one concatenated variable?

592 views Asked by At

I have been attempting to create an Azure Function with PowerShell based on what I mentioned in the title and have been getting the following error. I will also attach the code blocks from the function.json and run.ps1 files:

ERROR

2023-07-05T10:17:33.175 [Error] Executed 'Functions.HttpTrigger1' (Failed, Id=ef2aae22-df71-4027-b936-21fa2ef56f54, Duration=2ms)Result: FailureException: No input binding defined for the parameter 'res' that is declared in the script or function.Stack: at Microsoft.Azure.Functions.PowerShellWorker.AzFunctionInfo..ctor(RpcFunctionMetadata metadata) in /mnt/vss/_work/1/s/src/FunctionInfo.cs:line 134at Microsoft.Azure.Functions.PowerShellWorker.FunctionLoader.LoadFunction(FunctionLoadRequest request) in /mnt/vss/_work/1/s/src/FunctionLoader.cs:line 52at Microsoft.Azure.Functions.PowerShellWorker.RequestProcessor.ProcessFunctionLoadRequest(StreamingMessage request) in /mnt/vss/_work/1/s/src/RequestProcessor.cs:line 224

RUN.PS1

`using namespace System.Net

param($req, $res)

# Retrieve the values of the parameters from the parameter section
$name1 = $req.Query.name1
$name2 = $req.Query.name2

# Construct the response body with the parameter values
$responseBody = "$name1 $name2"

# Create the HTTP response
$res = [HttpResponseContext]@{
    StatusCode = [HttpStatusCode]::OK
    Body       = $responseBody
}

# Output the response to the output binding
$res`

FUNCTION.JSON

`{
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ],
  "disabled": false
}`

ADDTIONAL INFO Input Parameters

Any help with this issue would be greatly appreciated.

1

There are 1 answers

0
RithwikBojja On

I have reproduced in my environment and got expected results as below:

function.json:

{
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "Request",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "Response"
    }
  ]
}

run.ps:

using namespace System.Net

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

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$name1 = $Request.Query.name1
$name2 = $Request.Query.name2
$responseBody = "$name1  $name2"

    $body = "Hello, $responseBody. This HTTP triggered function executed successfully."


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

Input:

enter image description here

Output:

enter image description here

In this process(code), I have used Response(In Associate values to output ,part of code) as (Push-OutputBinding -Name Response) the variable which stores concatenated variable value.