1

I have created a very simple Google Cloud Run Python service to experiment with Restful APIs:

import os
from flask import Flask

app = Flask(__name__)

@app.route('/user/<user_name>', methods=['PUT'])
def create_user(user_name):
    return 'created!', 200

@app.route('/user/<user_name>', methods=['DEL'])
def delete_user(user_name):
    return 'deleted!', 200

if __name__ == "__main__":
    app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 8080)))

I have managed to successfully deploy this service to Cloud Run using this Dockerfile.

To test the service, I use curl to hit the deployed endpoint:

$curl -X PUT https://testapi.xxxxxxxx.a.run.app/user/prl900
created!

However, the DEL request causes a 502 error, and mysteriously this is not logged in Cloud Run's logging platform.

$curl -X DEL https://testapi.xxxxxxxx.a.run.app/user/prl900
...
...
<p><b>502.</b> <ins>That’s an error.</ins>
  <p>The server encountered a temporary error and could not complete your request.<p>Please try again in 30 seconds.  <ins>That’s all we know.</ins>

All other methods are rejected and are correctly logged in the backend, as expected:

$curl -X PATCH https://testapi.xxxxxxxx.a.run.app/user/prl900
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>405 Method Not Allowed</title>
<h1>Method Not Allowed</h1>
<p>The method is not allowed for the requested URL.</p>

Can anyone understand the reason for Cloud Run failing to process DEL requests and not logging them? Is there anything special about DEL requests on Cloud Run? The service works fine on my local computer.

Thanks for your help

prl900
  • 4,029
  • 4
  • 33
  • 40

1 Answers1

1

The correct HTTP verb is DELETE not DEL. You need to change your flask route to:

@app.route('/user/<user_name>', methods=['DELETE'])

and then the correct curl command will be:

curl -X DELETE https://testapi.xxxxxxxx.a.run.app/user/prl900

Using the non-standard verb is causing some proxy in the gcloud infra that is sitting in front of your service to refuse to route the request. This explains the 502 (bad gateway) and it why the logs don't show up (it never reaches your service).

Matthew
  • 12,892
  • 6
  • 42
  • 45
  • Thank you @Matthew this makes sense. It was confusing as the service worked on my local computer. – prl900 Mar 16 '21 at 00:53