Share via

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 31 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.


Answer accepted by question author

Ajay Kumar N 28,261 Reputation points Microsoft Employee Moderator
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!

Was this answer helpful?

0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

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