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 Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,773 questions
{count} votes

Accepted answer
  1. ajkuma 26,136 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 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.