|
1 |
| -# Invoked with Invoke-RestMethod: |
2 |
| -# irm http://localhost:7071/api/MyHttpTrigger?Name=Tyler |
3 |
| -# Input bindings are added via param block |
| 1 | +# Trigger the function by running Invoke-RestMethod: |
| 2 | +# (via get method): Invoke-RestMethod -Uri http://localhost:7071/api/MyHttpTrigger?Name=Joe |
| 3 | +# (via post method): Invoke-RestMethod ` |
| 4 | +# -Uri http://localhost:7071/api/MyHttpTrigger ` |
| 5 | +# -Method Post ` |
| 6 | +# -Body (ConvertTo-Json @{ Name="Joe" }) ` |
| 7 | +# -Headers @{'Content-Type' = 'application/json' }` |
4 | 8 |
|
| 9 | +# Input bindings are passed in via param block. |
5 | 10 | param($req, $TriggerMetadata)
|
6 | 11 |
|
7 |
| -# If no name was passed by query parameter |
8 |
| -$name = 'World' |
| 12 | +# You can write to the Azure Functions log streams as you would in a normal PowerShell script. |
| 13 | +Write-Verbose "PowerShell HTTP trigger function processed a request." -Verbose |
9 | 14 |
|
10 | 15 | # You can interact with query parameters, the body of the request, etc.
|
11 |
| -if($req.Query.Name) { |
12 |
| - $name = $req.Query.Name |
13 |
| -} |
14 |
| - |
15 |
| -# you can write to the same streams as you would in a normal PowerShell script |
16 |
| -Write-Verbose "Verbose $name" -Verbose |
17 |
| -Write-Warning "Warning $name" |
| 16 | +$name = $req.Query.Name |
| 17 | +if (-not $name) { $name = $req.Body.Name } |
18 | 18 |
|
19 |
| -# items in the pipeline get logged |
20 |
| -$name |
| 19 | +if($name) { |
| 20 | + $status = 200 |
| 21 | + $body = "Hello " + $name |
| 22 | +} |
| 23 | +else { |
| 24 | + $status = 400 |
| 25 | + $body = "Please pass a name on the query string or in the request body." |
| 26 | +} |
21 | 27 |
|
22 |
| -# You set the value of your output bindings by assignment `$nameOfOutputBinding = 'foo'` |
| 28 | +# You associate values to output bindings by calling 'Push-OutputBinding'. |
23 | 29 | Push-OutputBinding -Name res -Value ([HttpResponseContext]@{
|
24 |
| - Body = @{ Hello = $name } |
25 |
| - ContentType = 'application/json' |
| 30 | + StatusCode = $status |
| 31 | + Body = $body |
26 | 32 | })
|
| 33 | + |
0 commit comments