fastapi Unable to get request parameters in request #6593
-
First Check
Commit to Help
Example Codetest3.py
from pydantic import BaseModel
from fastapi import APIRouter
class CreateUser(BaseModel):
username: str
password: str
test=APIRouter()
@test.post("login")
def login(login:CreateUser):
return login
test2.py
import uvicorn
from fastapi import FastAPI,Request
from test3 import test
import time
app = FastAPI()
@app.middleware('http')
async def add_process_time_header(request: Request, call_next): # call_next将接收request请求做为参数
start_time = time.time()
query_param=request.query_params
print("query_param is null={}",query_param)
response = await call_next(request)
return response
app.include_router(router=test,prefix='/login', tags=['分析模块'])
if __name__ == '__main__':
uvicorn.run(app='test2:app', host="0.0.0.0", port=8002, reload=True, debug=True)DescriptionMy request class inherits pydantic's basemodel, and then uses middleware to obtain the request parameters when requesting. It is found that the request parameters are empty and query_ Params method, I hope you can answer Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.63.0 Python Version3.8 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 6 comments
-
|
Pydantic base models will go into the body fo the request - you could you check the swagger docs the app creates to confirm what the curl command it generates is? |
Beta Was this translation helpful? Give feedback.
-
I checked that there is no request body. You can see the example I wrote. In this example, no parameters are obtained, |
Beta Was this translation helpful? Give feedback.
-
@app.middleware("http")
async def add_process_time_header(request: Request, call_next): # call_next将接收request请求做为参数
start_time = time.time()
query_param = request.query_params
print(await request.json())
print("query_param is null={}", query_param)
response = await call_next(request)
return responseIt is in the body - see this |
Beta Was this translation helpful? Give feedback.
-
|
|
Beta Was this translation helpful? Give feedback.
-
|
@chenchen1992223 did this answer your question? If so could you close the issue 😄 |
Beta Was this translation helpful? Give feedback.
-
|
Thank you for your answer. Thank you.
|
Beta Was this translation helpful? Give feedback.
It is in the body - see this