Google Cloud Run 未记录 DEL 请求的 502 响应

时间:2021-03-15 11:49:59

标签: python gunicorn flask-restful google-cloud-run http-delete

我创建了一个非常简单的 Google Cloud Run Python 服务来试验 Restful API:

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)))

我已成功使用此 Dockerfile 将此服务部署到 Cloud Run。

为了测试服务,我使用 curl 来访问部署的端点:

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

但是,DEL 请求会导致 502 错误,而且奇怪的是,这并未记录在 Cloud Run 的日志记录平台中。

$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>

所有其他方法都被拒绝并按预期正确记录在后端:

$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>

谁能理解 Cloud Run 无法处理 DEL 请求且未记录它们的原因? Cloud Run 上的 DEL 请求有什么特别之处吗?该服务在我的本地计算机上运行良好。

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

正确的 HTTP 动词是 DELETE 而不是 DEL。您需要将您的烧瓶路线更改为:

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

然后正确的 curl 命令将是:

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

使用非标准动词会导致位于您的服务前面的 gcloud 基础设施中的某些代理拒绝路由请求。这解释了 502(坏网关)以及日志不显示的原因(它永远不会到达您的服务)。

相关问题