34

Edit:

I found the problem but not sure why this happens. Whenever I query: http://localhost:4001/hello/ with the "/" in the end - I get a proper 200 status response. I do not understand why.

Original Post:

Whenever I send a query to my app - I keep getting a 307 redirect. How to get my app to return regular status 200 instead of redirecting it through 307

This is the request output:

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 returns:

E       assert 307 == 200
E        +  where 307 = <Response [307]>.status_code

test_main.py:24: AssertionError

in my root dir: /__init__.py file:

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 file:

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"

in /Dockerfile:

CMD ["uvicorn", "abm:abm", "--reload", "--proxy-headers", "--host", "0.0.0.0", "--port", "4001", "--forwarded-allow-ips", "*", "--log-level", "debug"]

I tried with and without "--forwarded-allow-ips", "*" part.

1 Answer 1

66

It happens because the exact path defined by you for your view is yourdomainname/hello/, so when you hit it without / at the end, it first attempts to get to that path but as it is not available it checks again after appending / and gives a redirect status code 307 and then when it finds the actual path it returns the status code that is defined in the function/view linked with that path, i.e status code 200 in your case.

You can also read more about the issue here: https://github.com/tiangolo/fastapi/issues/2060#issuecomment-834868906

1
  • 1
    +1. for some odd reason when I was testing my API locally it was fine, but when deployed to AWS Lambda, i was getting infinite redirects. Thanks!
    – rezan21
    Mar 11 at 1:51

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.