編輯:
我發現了問題,但不確定為什么會發生這種情況。每當我查詢:最后http://localhost:4001/hello/是“ /”時 - 我會得到正確的 200 狀態回應。我不理解為什么。
原帖:
每當我向我的應用程式發送查詢時 - 我都會收到 307 重定向。如何讓我的應用回傳常規狀態 200 而不是通過 307 重定向它
這是請求輸出:
abm | INFO: 172.18.0.1:46476 - "POST /hello HTTP/1.1" 307 Temporary Redirect
abm | returns the apples data. nothing special here.
abm | INFO: 172.18.0.1:46480 - "POST /hello/ HTTP/1.1" 200 OK
pytest 回傳:
E assert 307 == 200
E where 307 = <Response [307]>.status_code
test_main.py:24: AssertionError
在我的根目錄中:/__init__.py檔案:
from fastapi import FastAPI
from fastapi.middleware.cors import CORSMiddleware
# from .configs import cors
from .subapp import router_hello
from .potato import router_potato
from .apple import router_apple
abm = FastAPI(
title = "ABM"
)
# potato.add_middleware(cors)
abm.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
abm.include_router(router_hello.router)
abm.include_router(router_potato.router)
abm.include_router(router_apple.router)
@abm.post("/test", status_code = 200)
def test():
print('test')
return 'test'
/subapp/router_hello.py 檔案:
router = APIRouter(
prefix='/hello',
tags=['hello'],
)
@router.post("/", status_code = 200)
def hello(req: helloBase, apple: appleHeader = Depends(set_apple_header), db: Session = Depends(get_db)) -> helloResponse:
db_apple = apple_create(apple, db, req.name)
if db_apple:
return set_hello_res(db_apple.potato.api, db_apple.name, 1)
else:
return "null"
在/Dockerfile:
CMD ["uvicorn", "abm:abm", "--reload", "--proxy-headers", "--host", "0.0.0.0", "--port", "4001", "--forwarded-allow-ips", "*", "--log-level", "debug"]
我試過有和沒有"--forwarded-allow-ips", "*"部分。
uj5u.com熱心網友回復:
這是因為通過您的視圖中定義的確切路徑
yourdomainname/hello/,所以當你打它沒有/底,它首先試圖去這條道路,但它再次無法訪問,檢查追加后/,并給出了重定向status code 307,然后當它找到實際路徑,它回傳在function/view與該路徑的鏈接中定義的狀態代碼,即status code 200在您的情況下。
您還可以在此處閱讀有關該問題的更多資訊:https : //github.com/tiangolo/fastapi/issues/2060#issuecomment-834868906
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/385147.html
標籤:Python 重定向 快点 http-status-code-307
上一篇:301重定向檔案夾根
