Support X-Forwarded-Prefix header #1386
-
Hi, What are your thoughts on supporting the |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 1 reply
-
For use-cases such as this I'd suggest implementing the behaviour as ASGI middleware inside your own codebase. |
Beta Was this translation helpful? Give feedback.
-
Ok, that works for me. Thank you for your opinion on this. |
Beta Was this translation helpful? Give feedback.
-
I would have also expected that the This page has two middleware solutions for this problem: https://ecm.community/t/fast-api-und-x-forwarded-prefix/192 by @uwohlfeil Copying the middleware class solution here: from typing import Union, cast
from starlette.types import ASGIApp, Scope, Receive, Send
from asgiref.typing import (
HTTPScope,
WebSocketScope,
)
class XPrefixHeaderFixMiddleWare:
def __init__(self, app: ASGIApp):
self.app = app
async def __call__(self, scope: Scope, receive: Receive, send: Send):
if scope["type"] in ("http", "websocket"):
scope = cast(Union[HTTPScope, WebSocketScope], scope)
headers = dict(scope["headers"])
if b"x-forwarded-prefix" in headers:
prefix = headers[b"x-forwarded-prefix"].decode("UTF-8")
scope['root_path'] = prefix
return await self.app(scope, receive, send) |
Beta Was this translation helpful? Give feedback.
For use-cases such as this I'd suggest implementing the behaviour as ASGI middleware inside your own codebase.