简体   繁体   English

Cloud Run 请求在未到达已部署服务的情况下触发错误 400

[英]Cloud Run requests triggers error 400 without reaching deployed service

I am currently using Google Cloud Run to deploy an API and although everything was working fine I am now having quite a hard time understanding an error I get.我目前正在使用Google Cloud Run部署 API,虽然一切正常,但我现在很难理解我遇到的错误。

I created the API on python3 using FastAPI , the Location model is based on Pydantic's BaseModel.我使用FastAPI在 python3 上创建了 API,Location 模型基于Pydantic 的BaseModel。

To illustrate it I have defined a test route as follow :为了说明这一点,我定义了一个测试路线如下:

class Location(BaseModel):
    lat: float
    lng: float


@router.get('/test_404')
async def test_404(origin: Location = Body(...),
                   destination: Location = Body(...)):
    print(origin)
    print(destination)
    return {'res': 'ok'}

The route should take two arguments in the request's body : origin and destination, and it does but only when I deploy it locally.路线应该用两个参数在请求的身体:始发地和目的地,以及,但只有当我在本地部署它。 This :这个 :

url_local = "http://0.0.0.0:8080/test_404"
data = {
    'origin': {'lat': 104, 'lng': 342},
    'destination': {'lat': 104, 'lng': 342}
}
resp = requests.get(url_local, json = data)
print(resp.text)

Outputs :输出:

'{"res":"ok"}'

The issue arises when I deploy the same service on Cloud Run.当我在 Cloud Run 上部署相同的服务时会出现问题。 All of my other routes work fine but here is the output I get when I use the code above with the container url :我的所有其他路由都可以正常工作,但这是我将上面的代码与容器 url 一起使用时得到的输出:

    <!DOCTYPE html>\n
    <html lang=en>
    <meta charset=utf-8>
    \n
    <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
    \n <title>Error 400 (Bad Request)!!1</title>\n
    \n <a href=//www.google.com/><span id=logo aria-label=Google></span></a>\n
    <p><b>400.</b>
        <ins>That’s an error.</ins>
        \n
    <p>Your client has issued a malformed or illegal request.
        <ins>That’s all we know.</ins>

This is an error triggered by google and that does not let my request reach the service (no log entry)这是由 google 触发的错误,它不会让我的请求到达服务(无日志条目)

I've searched online but did not find what causes this error.我在网上搜索过,但没有找到导致此错误的原因。 What am I missing ?我错过了什么?

Thank you very much非常感谢

It might be related to this post, where a user experiences a similar message.它可能与这篇文章有关,其中用户遇到了类似的消息。 After some reading, and seeing your example, I think the solution lies in the REST best practices .经过一些阅读并看到您的示例后,我认为解决方案在于 REST best practice

Strictly speaking, a GET request should not send any data when doing the request, other than anything incorporated within the URI.严格来说,GET 请求在执行请求时不应发送任何数据,除了 URI 中包含的任何数据。 Technically, everything is possible, but you should use query parameters instead of sending a body.从技术上讲,一切皆有可能,但您应该使用查询参数而不是发送正文。

Some API wrappers (like Cloud Run), may not accept this behaviour, while in your local test it may seem to work.某些 API 包装器(如 Cloud Run)可能不接受这种行为,而在您的本地测试中它似乎可以工作。 Other API wrappers may just accept a body with a GET request.其他 API 包装器可能只接受带有 GET 请求的主体。 I would recommend sticking to the REST best practices and you are fine.我建议坚持 REST 最佳实践,你很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM