HTTP Trigger (PowerShell)
This document describes how to configure and use the HTTP trigger for Azure Functions written in PowerShell.
Overview
The HTTP trigger allows your Azure Function to be invoked via an HTTP request. It's a fundamental building block for creating serverless APIs, webhooks, and microservices.
Configuration
To configure an HTTP trigger, you define it in your function's function.json file.
function.json example
            
{
  "scriptFile": "__init__.ps1",
  "bindings": [
    {
      "authLevel": "function",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "get",
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "res"
    }
  ]
}
            Key properties:
- scriptFile: The name of the PowerShell script file that contains your function code.
- type: Must be- httpTriggerfor the input binding.
- direction: Must be- infor an input trigger.
- name: The name of the variable that will hold the HTTP request object in your PowerShell script.
- methods: An array of HTTP methods that the trigger will respond to (e.g.,- get,- post).
- For the output binding:
                    - type: Must be- http.
- direction: Must be- out.
- name: The name of the variable that will hold the HTTP response object in your PowerShell script.
 
- authLevel: Determines the authorization level required to invoke the function. Common values include:- anonymous: No authentication required.
- function: Requires a function key.
- admin: Requires an admin key.
 
PowerShell Script (__init__.ps1)
            The PowerShell script receives the HTTP request as the $req object and must return an HTTP response via the $res object.
Basic Example
param($req)
# Get query parameters
$name = $req.Query.name
# Get request body
$body = $req.Body | ConvertFrom-Json
if ($name) {
    $responseMessage = "Hello, $name"
} elseif ($body -and $body.name) {
    $responseMessage = "Hello, $($body.name)"
} else {
    $responseMessage = "Hello, World!"
}
# Create the HTTP response
$res = @{
    StatusCode = 200
    Body = $responseMessage
}
# Return the response object
return $res
            Accessing Request Details
The $req object provides access to various parts of the HTTP request:
- $req.Method: The HTTP method used (e.g., "GET", "POST").
- $req.Headers: A hashtable of request headers.
- $req.Query: A hashtable of query string parameters.
- $req.Body: The request body. Use- ConvertFrom-Jsonfor JSON payloads.
- $req.Params: A combination of query parameters and route parameters.
Creating the Response
The $res hashtable should contain the following properties:
- StatusCode: The HTTP status code (e.g., 200, 400, 404).
- Body: The content of the response body. Can be a string, object, or stream.
- Headers: An optional hashtable of response headers.
- ContentType: An optional string to set the- Content-Typeheader (e.g., "application/json", "text/plain").
Example with JSON Response
param($req)
$data = @{
    message = "Processing complete"
    timestamp = (Get-Date).ToString("yyyy-MM-ddTHH:mm:ssZ")
}
$res = @{
    StatusCode = 200
    Headers = @{ 'Content-Type' = 'application/json' }
    Body = $data | ConvertTo-Json
}
return $res
            Security and Authorization
The authLevel property in function.json is crucial for securing your HTTP-triggered functions. When set to function, you'll need to include a function key in your request URL or headers.
Example URL with function key:
https://.azurewebsites.net/api/MyHttpFunction?code=
              Troubleshooting
- Check your function.jsonfor syntax errors.
- Ensure the scriptFilename matches your PowerShell script.
- Verify that your PowerShell script correctly handles the $reqobject and returns a valid$resobject.
- Use the Azure Functions portal's "Monitor" tab for logs and error messages.