-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInvoke-QuickRequest.ps1
More file actions
192 lines (158 loc) · 5.39 KB
/
Copy pathInvoke-QuickRequest.ps1
File metadata and controls
192 lines (158 loc) · 5.39 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<#
.SYNOPSIS
Quick HTTP requests from PowerShell.
.DESCRIPTION
Make HTTP requests without leaving the terminal.
Supports GET, POST, PUT, DELETE, PATCH methods.
.PARAMETER Method
HTTP method (GET, POST, PUT, DELETE, PATCH).
.PARAMETER Url
The URL to request.
.PARAMETER Body
Request body (hashtable or string). Automatically converts to JSON for POST/PUT/PATCH.
.PARAMETER Headers
Additional headers as hashtable.
.PARAMETER AsJson
Return response as parsed JSON object.
.PARAMETER Raw
Return raw response content only.
.EXAMPLE
http GET http://localhost:3000/api/health
http POST http://localhost:3000/api/users -Body @{name='test'}
http GET $url -AsJson
http PUT http://localhost:3000/api/users/1 -Body @{name='updated'} -Headers @{Authorization='Bearer token'}
#>
[CmdletBinding()]
param(
[Parameter(Position = 0)]
[ValidateSet('GET', 'POST', 'PUT', 'DELETE', 'PATCH', 'HEAD', 'OPTIONS')]
[string]$Method = 'GET',
[Parameter(Position = 1, Mandatory = $true)]
[string]$Url,
[Parameter(Position = 2)]
$Body,
[hashtable]$Headers = @{},
[switch]$AsJson,
[switch]$Raw
)
# Build parameters
$params = @{
Uri = $Url
Method = $Method
UseBasicParsing = $true
ErrorAction = 'Stop'
}
# Add headers
$defaultHeaders = @{
'Accept' = 'application/json'
'User-Agent' = 'PowerShell-QuickRequest/1.0'
}
foreach ($key in $Headers.Keys) {
$defaultHeaders[$key] = $Headers[$key]
}
$params.Headers = $defaultHeaders
# Add body for POST/PUT/PATCH
if ($Body -and $Method -in @('POST', 'PUT', 'PATCH')) {
if ($Body -is [hashtable] -or $Body -is [PSCustomObject]) {
$params.Body = $Body | ConvertTo-Json -Depth 10
$params.ContentType = 'application/json'
} else {
$params.Body = $Body
}
}
try {
$stopwatch = [System.Diagnostics.Stopwatch]::StartNew()
$response = Invoke-WebRequest @params
$stopwatch.Stop()
$contentType = $response.Headers['Content-Type'] -join ''
$isJson = $contentType -like '*json*'
# Raw output
if ($Raw) {
return $response.Content
}
# JSON output for MCP tools
if ($AsJson) {
$result = [ordered]@{
status = $response.StatusCode
statusDescription = $response.StatusDescription
contentType = $contentType
contentLength = $response.Content.Length
elapsed = "$($stopwatch.ElapsedMilliseconds)ms"
}
if ($isJson) {
try {
$result.body = $response.Content | ConvertFrom-Json
} catch {
$result.body = $response.Content
}
} else {
$result.body = $response.Content
}
return $result | ConvertTo-Json -Depth 10
}
# Pretty output
Write-Host ""
Write-Host "$Method " -NoNewline -ForegroundColor Cyan
Write-Host $Url -ForegroundColor White
Write-Host ""
# Status line
$statusColor = if ($response.StatusCode -lt 300) { 'Green' }
elseif ($response.StatusCode -lt 400) { 'Yellow' }
else { 'Red' }
Write-Host "Status: " -NoNewline -ForegroundColor Gray
Write-Host "$($response.StatusCode) $($response.StatusDescription)" -ForegroundColor $statusColor
Write-Host "Time: " -NoNewline -ForegroundColor Gray
Write-Host "$($stopwatch.ElapsedMilliseconds)ms" -ForegroundColor White
Write-Host "Type: " -NoNewline -ForegroundColor Gray
Write-Host $contentType -ForegroundColor White
Write-Host ""
# Response body
if ($response.Content) {
if ($isJson) {
try {
$parsed = $response.Content | ConvertFrom-Json
$formatted = $parsed | ConvertTo-Json -Depth 10
Write-Host $formatted -ForegroundColor Yellow
} catch {
Write-Host $response.Content
}
} else {
# Truncate long responses
if ($response.Content.Length -gt 2000) {
Write-Host ($response.Content.Substring(0, 2000)) -ForegroundColor White
Write-Host "`n... (truncated, $($response.Content.Length) bytes total)" -ForegroundColor DarkGray
} else {
Write-Host $response.Content -ForegroundColor White
}
}
}
Write-Host ""
} catch {
$errorResponse = $_.Exception.Response
if ($AsJson) {
$result = [ordered]@{
status = if ($errorResponse) { [int]$errorResponse.StatusCode } else { 0 }
error = $_.Exception.Message
}
# Try to get response body on error
if ($errorResponse) {
try {
$reader = [System.IO.StreamReader]::new($errorResponse.GetResponseStream())
$result.body = $reader.ReadToEnd()
$reader.Close()
} catch {}
}
return $result | ConvertTo-Json -Depth 5
}
Write-Host ""
Write-Host "Request Failed" -ForegroundColor Red
Write-Host ""
if ($errorResponse) {
Write-Host "Status: " -NoNewline -ForegroundColor Gray
Write-Host "$([int]$errorResponse.StatusCode) $($errorResponse.StatusDescription)" -ForegroundColor Red
}
Write-Host "Error: " -NoNewline -ForegroundColor Gray
Write-Host $_.Exception.Message -ForegroundColor Red
Write-Host ""
exit 1
}