Middleware Response status

Product: PowerShell Universal
Version: 4.3.0

I am trying to retrieve response status code of each Endpoints calls. I have added middleware.ps1 file and used New-PSUMiddleware but I cannot retrieve the correct statuscode from the responses : $HttpContext.response.statuscode always have value 200 for all responses.

Does anyone know how to retrieve the status response code from an Endpoint without having to add code to the Endpoint itself?

Here the code :

.universal/middleware.ps1

New-PSUMiddleware -Name 'Middle' -ScriptBlock {
    param($HttpContext)
$HttpContext.Response | convertto-json | Out-File -FilePath "C:\logs\HttpContext.txt" -Append
}

.universal/endpoints.ps1


New-PSUEndpoint -Url "/test" -Method @('POST') -Endpoint {
    Param(
        [Parameter(Mandatory = $true)]
        [string]$mymandatoryvar
    )

    $result = "i'm a teapot"
 
    return $result
}

Exemple :
API POST Call without mymandatoryvar cause status code error 400 but Middleware always respond status code 200 :


"Response": {
      "HttpContext": "Microsoft.AspNetCore.Http.DefaultHttpContext",
      "StatusCode": 200,
      "Headers": "",
      "Body": "Microsoft.AspNetCore.ResponseCompression.ResponseCompressionBody",
      "ContentLength": null,
      "ContentType": null,
      "Cookies": "Microsoft.AspNetCore.Http.ResponseCookies",
      "HasStarted": false,
      "BodyWriter": "System.IO.Pipelines.StreamPipeWriter"
    },

Middleware runs before the request and not after so you won’t see the status code in the middleware.

Hi

Thank you very much for this clarification :blush:.

Is there way to intercept endpoints return codes like a trigger event could do on a success and error result (actually there is a way to create event only on error endpoint result) ? or like a middleware if it is run after the request ?

We create an audit log for all API calls that include the status code being returned to the user. Having it on the outbound would be great, define code in one place instead of every end point