-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic-get-requests.ps1
More file actions
149 lines (135 loc) · 6.51 KB
/
Copy pathbasic-get-requests.ps1
File metadata and controls
149 lines (135 loc) · 6.51 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# Basic GET Request Examples
# Simple examples for making HTTP GET requests to APIs
#
# WHAT YOU CAN MODIFY:
# - URLs: Replace with your actual API endpoints
# - Headers: Add/remove headers as needed for your API
# - Query parameters: Adjust to match your API requirements
# - Timeout values: Set based on your API's expected response time
#
# WHAT YOU SHOULDN'T CHANGE:
# - The basic Invoke-RestMethod structure
# - Error handling patterns (try/catch blocks)
# - Security practices (HTTPS, timeouts)
#
# SECURITY CONSIDERATIONS:
# - Always use HTTPS endpoints in production
# - Never include sensitive data in URLs (use headers or POST body)
# - Set appropriate timeouts to prevent hanging requests
# - Validate responses before using the data
Write-Host "=== Basic GET Request Examples ===" -ForegroundColor Green
# Example 1: Simple GET request
# MODIFY: Change the URL to your API endpoint
# KEEP: The basic structure and error handling
Write-Host "`n1. Simple GET request:" -ForegroundColor Yellow
try {
$response = Invoke-RestMethod -Uri "https://httpbin.org/get" -Method GET
# MODIFY: Change how you handle the response data
Write-Host "✅ Success! Your IP: $($response.origin)" -ForegroundColor Green
Write-Host "User-Agent: $($response.headers.'User-Agent')" -ForegroundColor Cyan
} catch {
# DON'T CHANGE: Always handle errors like this
Write-Error "❌ Request failed: $($_.Exception.Message)"
}
# Example 2: GET with custom headers
# MODIFY: Add headers required by your API (Authorization, API keys, etc.)
# SECURITY: Never hardcode API keys - use environment variables
Write-Host "`n2. GET with custom headers:" -ForegroundColor Yellow
$customHeaders = @{
# MODIFY: Replace with headers your API needs
"User-Agent" = "YourApp/1.0" # Change to your app name
"Accept" = "application/json" # Keep this for JSON APIs
"X-Custom-Header" = "YourValue" # Replace with actual headers
# SECURITY: For auth headers, use: "Authorization" = "Bearer $env:API_TOKEN"
}
try {
# MODIFY: Replace URL with your endpoint
$response = Invoke-RestMethod -Uri "https://httpbin.org/headers" -Headers $customHeaders -Method GET
Write-Host "✅ Success! Headers sent:" -ForegroundColor Green
# MODIFY: Change how you display results
$response.headers | ConvertTo-Json | Write-Host -ForegroundColor Cyan
} catch {
Write-Error "❌ Request failed: $($_.Exception.Message)"
}
# Example 3: GET with query parameters
# MODIFY: Change parameters to match your API's requirements
# SECURITY: Be careful with sensitive data in query strings
Write-Host "`n3. GET with query parameters:" -ForegroundColor Yellow
$queryParams = @{
# MODIFY: Replace with your API's parameters
name = "John Doe"
age = 30
city = "New York"
# SECURITY WARNING: Never put passwords or API keys in query parameters!
}
# DON'T CHANGE: This builds query strings safely
$queryString = ($queryParams.GetEnumerator() | ForEach-Object { "$($_.Key)=$($_.Value)" }) -join "&"
$url = "https://httpbin.org/get?$queryString" # MODIFY: Change base URL
try {
$response = Invoke-RestMethod -Uri $url -Method GET
Write-Host "✅ Success! Parameters received:" -ForegroundColor Green
$response.args | ConvertTo-Json | Write-Host -ForegroundColor Cyan
} catch {
Write-Error "❌ Request failed: $($_.Exception.Message)"
}
# Example 4: GET with timeout control
# MODIFY: Adjust timeout based on your API's typical response time
# KEEP: Always set timeouts to prevent hanging
Write-Host "`n4. GET with timeout control:" -ForegroundColor Yellow
try {
# MODIFY: Change timeout value (in seconds) based on your needs
# Fast APIs: 5-10 seconds, Slow APIs: 30-60 seconds
$response = Invoke-RestMethod -Uri "https://httpbin.org/get" -TimeoutSec 10
Write-Host "✅ Success within timeout!" -ForegroundColor Green
} catch {
Write-Error "❌ Request failed: $($_.Exception.Message)"
}
# Example 5: Handling different response formats
# MODIFY: Adjust based on your API's response format
Write-Host "`n5. Handling JSON vs XML responses:" -ForegroundColor Yellow
# JSON response (most common)
try {
$jsonResponse = Invoke-RestMethod -Uri "https://httpbin.org/json" -Method GET
Write-Host "✅ JSON Response received:" -ForegroundColor Green
# MODIFY: Change how you process JSON data
Write-Host "Sample data: $($jsonResponse.slideshow.title)" -ForegroundColor Cyan
} catch {
Write-Error "❌ JSON request failed: $($_.Exception.Message)"
}
# XML response (less common)
try {
$xmlResponse = Invoke-RestMethod -Uri "https://httpbin.org/xml" -Method GET
Write-Host "✅ XML Response received:" -ForegroundColor Green
# MODIFY: Change how you process XML data
Write-Host "XML root element: $($xmlResponse.LocalName)" -ForegroundColor Cyan
} catch {
Write-Error "❌ XML request failed: $($_.Exception.Message)"
}
# Example 6: Real API with error handling
# MODIFY: Replace with your actual API endpoint
Write-Host "`n6. Real API example with comprehensive error handling:" -ForegroundColor Yellow
try {
# MODIFY: Replace with your API URL
$response = Invoke-RestMethod -Uri "https://api.github.com/zen" -Method GET
Write-Host "✅ API Response: $response" -ForegroundColor Green
} catch [System.Net.WebException] {
# DON'T CHANGE: This handles HTTP errors properly
$statusCode = $_.Exception.Response.StatusCode
Write-Error "❌ HTTP Error $statusCode: $($_.Exception.Message)"
} catch {
# DON'T CHANGE: This catches all other errors
Write-Error "❌ Unexpected error: $($_.Exception.Message)"
}
Write-Host "`n=== Key Points for Modification ===" -ForegroundColor Magenta
Write-Host "✏️ MODIFY THESE:" -ForegroundColor Yellow
Write-Host " • URLs - Replace with your actual API endpoints" -ForegroundColor White
Write-Host " • Headers - Add authentication and required headers" -ForegroundColor White
Write-Host " • Parameters - Match your API's requirements" -ForegroundColor White
Write-Host " • Timeouts - Set based on your API's performance" -ForegroundColor White
Write-Host " • Response handling - Process data for your needs" -ForegroundColor White
Write-Host "`n🔒 SECURITY REMINDERS:" -ForegroundColor Red
Write-Host " • Use HTTPS endpoints only" -ForegroundColor White
Write-Host " • Store API keys in environment variables" -ForegroundColor White
Write-Host " • Never put sensitive data in query parameters" -ForegroundColor White
Write-Host " • Always set timeouts" -ForegroundColor White
Write-Host " • Validate all response data" -ForegroundColor White