forked from Badgerati/Pode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrest-api.ps1
32 lines (24 loc) · 1.18 KB
/
rest-api.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop
# or just:
# Import-Module Pode
# create a server, and start listening on port 8086
Start-PodeServer {
Add-PodeEndpoint -Address * -Port 8086 -Protocol Http
# can be hit by sending a GET request to "localhost:8086/api/test"
Add-PodeRoute -Method Get -Path '/api/test' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'hello' = 'world'; }
}
# can be hit by sending a POST request to "localhost:8086/api/test"
Add-PodeRoute -Method Post -Path '/api/test' -ContentType 'application/json' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'hello' = 'world'; 'name' = $WebEvent.Data['name']; }
}
# returns details for an example user
Add-PodeRoute -Method Get -Path '/api/users/:userId' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'user' = $WebEvent.Parameters['userId']; }
}
# returns details for an example user
Add-PodeRoute -Method Get -Path '/api/users/:userId/messages' -ScriptBlock {
Write-PodeJsonResponse -Value @{ 'user' = $WebEvent.Parameters['userId']; }
}
}