PowerShell Universal
v4
Ask or search…
K

Middleware

Customize HTTP requests in PowerShell Universal.
Middleware is run during each HTTP request by the PowerShell Universal server. It gives you the opportunity to customize features of the request like status code, headers and cookies.
Since middleware is executed during each request, performance should be a consideration when implementing custom middleware.

Middleware.ps1

The middleware.ps1 file can be created in the .universal folder using the New-PSUMiddleware cmdlet.
Each middleware instance receives a HttpContext object when it is called. The middleware should return true or false. If false is returned, the middleware will stop passing the context to additional items on the middleware pipeline.
The below example adds a test cookie to the response.
New-PSUMiddleware -Name 'Middle' -ScriptBlock {
param($HttpContext)
$HttpContext.Response.Cookies.Append('X-Test', "Test")
$true
}