@@ -291,3 +291,46 @@ def test_silence_routes_logging(backend, expected_loggers):
291291 assert (
292292 logger .level == logging .ERROR
293293 ), f"Logger { logger_name } should be set to ERROR level for { backend } backend"
294+
295+
296+ def test_fastapi_custom_post_route (dash_duo ):
297+ """Test that user-defined POST routes work with FastAPI backend.
298+
299+ Regression test for https://github.com/plotly/dash/issues/3801
300+ The DashMiddleware was consuming the request body for all routes,
301+ causing POST requests to user-defined routes to hang.
302+ """
303+ from fastapi import FastAPI , Request
304+ from fastapi .responses import JSONResponse
305+ import requests
306+
307+ fastapi_app = FastAPI ()
308+
309+ @fastapi_app .get ("/api/echo" )
310+ async def echo_get ():
311+ return JSONResponse ({"method" : "GET" , "ok" : True })
312+
313+ @fastapi_app .post ("/api/echo" )
314+ async def echo_post (request : Request ):
315+ body = await request .json ()
316+ return JSONResponse ({"echo" : body })
317+
318+ app = Dash (__name__ , server = fastapi_app )
319+ app .layout = html .Div ("Dash is running" )
320+
321+ dash_duo .start_server (app )
322+
323+ # Test GET request
324+ url = dash_duo .server_url
325+ resp = requests .get (f"{ url } /api/echo" , timeout = 5 )
326+ assert resp .status_code == 200
327+ assert resp .json () == {"method" : "GET" , "ok" : True }
328+
329+ # Test POST request - this was hanging before the fix
330+ resp = requests .post (
331+ f"{ url } /api/echo" ,
332+ json = {"hello" : "world" },
333+ timeout = 5 ,
334+ )
335+ assert resp .status_code == 200
336+ assert resp .json () == {"echo" : {"hello" : "world" }}
0 commit comments