githubEdit

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.

circle-exclamation

Middleware.ps1

The middleware.ps1 file can be created in the .universal folder using the New-PSUMiddleware cmdlet.

Each middleware instance receives a HttpContextarrow-up-right 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
}

Last updated

Was this helpful?