I am using a middleware to print the HTTP request body to avoid print statements in every function. However, there is no response from fastapi when running the client code.
The server is simplified to the code below:
import typing
import uvicorn
from fastapi import FastAPI
from fastapi import Request, Body
app = FastAPI()
@app.middleware('http')
async def debug_request(request: Request, call_next):
_body = await request.body()
print(_body)
#
response = await call_next(request)
return response
@app.put("/")
def _(
_body: typing.Dict
):
# print(_body) # this statement is replaced by the middleware
return {"detail": "ok"}
if __name__ == '__main__':
uvicorn.run(app, host='localhost', port=8000)
The client code is given below:
import requests
_url = 'http://localhost:8000/'
_json = {
'row_id': '1'
}
resp = requests.put(_url, json=_json)
if not resp.ok:
print('http-code: ', resp.status_code)
print('http-response: ', resp.text)
I don't have a solution for this yet, but, I have spent a fair amount of time stuck in this hanging issue (for critical apps in my org which have multiple custom MDW). This hanging is basically because @app.middleware("http") based middlewares, are in back-end created by inheriting from Starlette's BaseHTTPMiddleware. So this problem also exists for MDWs written by inheriting from BaseHTTPMiddleware explicitly. The reasons for this are quite complex and this is what I have understood so far:
StreamingResponse which has some issuesrequest.json() is allowed only once in request life-cycle in API, and BaseHTTPMiddleware also creates a Request object on its own (which causes hanging issue, since that is another request)The last link also mentions that what also causes the hanging issue, is that, because of StreamingResponse; the reading of response somehow gets exhausted in the first read, and when it comes to the second read, it keeps on waiting for it indefinitely which causes the hanging. (first and second read here means: in ASGI APPs, messages are sent to-and-from client & APP with various types like http.response.start, http.response.body etc)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With