flaskgoogle-cloud-runflask-restx

Flask redirects POST requests automatically to GET requests but only in the cloud


I've built a Flask API using Flask RESTX. I've run it locally in a container and it's working as expected. My endpoints are expecting POST requests:

class MyAPIEndpoint(Resource):
    def post(self):
        return "hello"

Quite simple. When I run the Flask app (using gunicorn or unsafe app.run() directly, doesn't matter) in Google Cloud Run, I get a `405 Method Not Allowed'. In the console, I can see that the requests have been redirected to a GET request, which I didn't allow on my endpoint above.

The log:

POST 308 1.04KB 5ms PostmanRuntime/7.31.0 <url>/myendpoint
POST 302 0B 0ms PostmanRuntime/7.31.0 <url>/myendpoint
GET 405 735B 3ms PostmanRuntime/7.31.0 <url>/myendpoint

As you can see, the requests are being redirected. I don't know why it's doing this, I haven't changed the docker image I'm using locally. There are no non-default configurations for the Google Cloud Run Service deployment. What's interesting is that the login and admin page (built with Flask Admin) works fine.

Does anyone know what's going on?

edit: I think that Flask itself triggers the 308 Redirect, cause it logs the 308 to the info log channel. Then there are the other log messages as shown above. I can also see in the logs that the body got dropped, so all post data got lost on the redirect.


Solution

  • It was a Google Cloud Run problem - I had to create an API gateway to enable API functionality.