Design a custom .NET Core middleware.
Asked in the Round 1 HackerRank stage. Implement the middleware behavior as a pure function for grading.
Given a request and simulated downstream result, return a response. Preserve an existing X-Correlation-ID header or use generated when it is absent. Requests whose path starts with /api/ require a non-empty Authorization header and otherwise return status 401 without using the downstream result. If downstream processing fails, return status 500. Otherwise, pass through the downstream status and body while including the correlation ID.
request is a dictionary containing a string path and optional headers./api/.downstream contains integer status, string body, and boolean error fields.def custom_middleware(request, downstream):