fastapi(starlette)RedirectResponse 重定向到 post 而不是获取方法 [重复]
- 2025-03-26 09:08:00
- admin 原创
- 16
问题描述:
返回 RedirectResponse 对象后,我遇到了奇怪的重定向行为
事件.py
router = APIRouter()
@router.post('/create', response_model=EventBase)
async def event_create(
request: Request,
user_id: str = Depends(get_current_user),
service: EventsService = Depends(),
form: EventForm = Depends(EventForm.as_form)
):
event = await service.post(
...
)
redirect_url = request.url_for('get_event', **{'pk': event['id']})
return RedirectResponse(redirect_url)
@router.get('/{pk}', response_model=EventSingle)
async def get_event(
request: Request,
pk: int,
service: EventsService = Depends()
):
....some logic....
return templates.TemplateResponse(
'event.html',
context=
{
...
}
)
路由器.py
api_router = APIRouter()
...
api_router.include_router(events.router, prefix="/event")
此代码返回结果
127.0.0.1:37772 - "POST /event/22 HTTP/1.1" 405 Method Not Allowed
好的,我看到出于某种原因调用了 POST 请求而不是 GET 请求。我搜索了解释,发现 RedirectResponse 对象默认为代码 307 并调用 POST链接
我按照建议添加了状态
redirect_url = request.url_for('get_event', **{'pk': event['id']}, status_code=status.HTTP_302_FOUND)
并得到
starlette.routing.NoMatchFound
为了实验,我将@router.get('/{pk}', response_model=EventSingle)
改为@router.post('/{pk}', response_model=EventSingle)
重定向成功完成,但此处的发布请求不适合我。我做错了什么?
更新型多巴胺
用于运行事件/创建逻辑的 html 表单
基础.html
<form action="{{ url_for('event_create')}}" method="POST">
...
</form>
基础视图.py
@router.get('/', response_class=HTMLResponse)
async def main_page(request: Request,
activity_service: ActivityService = Depends()):
activity = await activity_service.get()
return templates.TemplateResponse('base.html', context={'request': request,
'activities': activity})
解决方案 1:
当您想要在 POST 之后重定向到 GET 时,最佳做法是使用状态代码进行重定向303
,因此只需将代码更新为:
# ...
return RedirectResponse(redirect_url, status_code=303)
正如您所注意到的,重定向307
会保留 HTTP 方法和主体。
完整工作示例:
from fastapi import FastAPI, APIRouter, Request
from fastapi.responses import RedirectResponse, HTMLResponse
router = APIRouter()
@router.get('/form')
def form():
return HTMLResponse("""
<html>
<form action="/event/create" method="POST">
<button>Send request</button>
</form>
</html>
""")
@router.post('/create')
async def event_create(
request: Request
):
event = {"id": 123}
redirect_url = request.url_for('get_event', **{'pk': event['id']})
return RedirectResponse(redirect_url, status_code=303)
@router.get('/{pk}')
async def get_event(
request: Request,
pk: int,
):
return f'<html>oi pk={pk}</html>'
app = FastAPI(title='Test API')
app.include_router(router, prefix="/event")
要运行,请安装pip install fastapi uvicorn
并运行:
uvicorn --reload --host 0.0.0.0 --port 3000 example:app
然后,将浏览器指向:http://localhost:3000/event/form
解决方案 2:
您在此处提到的错误是因为你试图event_create
通过http://127.0.0.1:8000/event/create访问端点而引发的。但是,由于event_create
路由处理POST
请求,您的请求最终会到达get_event
端点(并引发value is not a valid integer
错误,因为您传递的是字符串而不是整数),因为当您在浏览器的地址栏中键入 URL 时,它会执行GET
请求。
因此,您需要一个HTML<form>
,例如,POST
向event_create
端点提交请求。下面是一个工作示例,您可以使用它访问 HTML (根据需要调整端口号)以发送<form>
请求,然后触发。http://127.0.0.1:8000/event/
`POST`RedirectResponse
正如 @tiangolo在这里提到的,当执行RedirectResponse
从POST
请求路由到GET
请求路由时,响应状态代码必须更改为303 See Other
。例如:
return RedirectResponse(redirect_url, status_code=status.HTTP_303_SEE_OTHER)
工作示例:
from fastapi import APIRouter, FastAPI, Request, status
from fastapi.responses import RedirectResponse, HTMLResponse
router = APIRouter()
# This endpoint can be accessed at http://127.0.0.1:8000/event/
@router.get('/', response_class=HTMLResponse)
def event_create_form(request: Request):
return """
<html>
<body>
<h1>Create an event</h1>
<form method="POST" action="/event/create">
<input type="submit" value="Create Event">
</form>
</body>
</html>
"""
@router.post('/create')
def event_create(request: Request):
event = {"id": 1}
redirect_url = request.url_for('get_event', **{'pk': event['id']})
return RedirectResponse(redirect_url, status_code=status.HTTP_303_SEE_OTHER)
@router.get('/{pk}')
def get_event(request: Request, pk: int):
return {"pk": pk}
app = FastAPI()
app.include_router(router, prefix="/event")