-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathGet-WebRequestBody.ps1
37 lines (30 loc) · 995 Bytes
/
Get-WebRequestBody.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
33
34
35
36
37
<#
.SYNOPSIS
Listens on a given port of the localhost, returning the body of a single web request, and responding with an empty 204.
.LINK
Start-HttpListener.ps1
.LINK
Stop-HttpListener.ps1
.LINK
Receive-WebRequest.ps1
.LINK
Read-WebRequest.ps1
.EXAMPLE
Get-WebRequestBody.ps1
(Returns the body of one request to http://localhost:8080/ as a byte array.)
#>
#Requires -Version 3
[CmdletBinding()][OutputType([byte[]])] Param(
# The local port to listen on.
[int] $Port = 8080
)
$http = Start-HttpListener.ps1 -Port $Port
$context = Receive-WebRequest.ps1 $http
$context.Request.Headers.Keys |ForEach-Object {Write-Verbose "${_}: $($context.Request.Headers[$_])"}
$readbytes =
if((Get-Command Get-Content).Parameters.Encoding.ParameterType -eq [Text.Encoding]) {@{AsByteStream=$true}}
else {@{Encoding='Byte'}}
Read-WebRequest.ps1 $context.Request @readbytes
$context.Response.StatusCode = 204
$context.Response.Close()
Stop-HttpListener.ps1 $http