FastAPI running on Azure App Service returns 405 Method Not Allowed in response to POST when the App Service is set to HTTPS Only

Tom Burgess 21 Reputation points
2022-02-14T14:37:11.21+00:00

As above, with the FastAPI app running with HTTPS Only set to false, POST requests succeed using https. As soon as I set HTTPS Only to true, a POST request to the same endpoint returns a 405 Method Not Allowed.

Azure App Services
Azure App Services
A feature of Azure App Service used to create and deploy scalable, mission-critical web apps.
4,551 questions
{count} votes

Accepted answer
  1. ajkuma 14,086 Reputation points Microsoft Employee
    2022-02-25T06:36:19.47+00:00

    To benefit the community, summarizing the answer here; from comments (above).

    HttpsOnly will only impact HTTP request (App Service Load Balancer will redirect HTTP to HTTPs).
    If the incoming is already HTTPs – then no-op. You mentioned the call is always HTTPs – so HTTPsOnly should not matter.

    From the investigations - it seems to be a combination of POSTMAN and your code issue. When you redirect /users to /users/ - it always redirect to HTTP regardless of incoming http or https. You may try to change the code when redirect /users to /users/ to preserve HTTP/HTTPS incoming scheme (http -> http, https -> https).

    @Tom Burgess

    • (mentioned in the private comment):

    "
    thanks for the response, it has led me to the answer I required.
    I was using the prefix parameter of the APIRouter class as per https://fastapi.tiangolo.com/tutorial/bigger-applications/#apirouter. It appears that this was responsible for the 307 redirect on HTTP.
    I'm still using that class but now without the prefix parameter. If I find that I require that in the future I have found this article to assist with that
    https://stackoverflow.com/questions/63511413/fastapi-redirection-for-trailing-slash-returns-non-ssl-link
    "

    Thanks again @Tom Burgess for your patience and cooperation!

0 additional answers

Sort by: Most helpful