Google端点获取请求URL参数[Python]

2024-10-02 00:36:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我目前正在做一个小项目,我需要写一个GET处理程序。我在使用端点文档中提供的Echo示例。在

我有我的资源容器:

GET_EMAIL_RESOURCE = endpoints.ResourceContainer(
    message_types.VoidMessage,
    i = messages.IntegerField(1, default = 1)
)

我有我的处理者:

^{pr2}$

为了访问这个,我使用curl:

curl --request GET --header "Content-Type: application/json" http://localhost:8080/_ah/api/echo/v1/echo/getEmails/3

现在这一切正常,并返回您期望的结果,但如果我需要将更多信息编码到我的URL中,例如:

getEmails/value1=someValue&value2=someOtherValue

我不知道如何做到这一点,也无法在文档中找到解释或示例。在

任何帮助都将不胜感激。在

编辑1:

我的代码现在看起来是这样的:

# main.py

GET_EMAIL_RESOURCE = endpoints.ResourceContainer(
    message_types.VoidMessage,
    i = messages.IntegerField(1, default = 1)
)

@endpoints.method(
    GET_EMAIL_RESOURCE,
    EchoResponse,
    path='echo/getEmails',
    http_method='GET',
    name='echo_get_emails'
)
def echo_get_emails(self, request):
    out = str(request.trailingDigits) + " : " +str(request.leadingDigits)
    return EchoResponse(content = out)

在我的openapi.json在我的道路下:

"/echo/v1/echo/getEmails" : {
  "get": {
    "operationId": "EchoApi_getEmails",
    "parameters" : [
      {
        "name": "trailingDigits",
        "in": "query",
        "description": "Trailing digits",
        "required": true,
        "type": "string"
      },
      {
        "name": "leadingDigits",
        "in": "query",
        "description": "Leading digits",
        "required": true,
        "type": "string"
      }
    ]
  }
}

但当我执行请求时 curl--request GET--header“Content Type:application/json”http://localhost:8080/_ah/api/echo/v1/echo/getEmails?trailingDigits=2&leadingDigits=2

我获取trailingDigits的内容,但是leadingDigits的默认值


Tags: nameechojsonhttpgetemailrequestcurl
3条回答

ResourceContainer中未在path值中使用的任何字段都应成为查询参数。在

为了修改参数,您需要配置openapi.json所需路径的文件。下面是一个例子:

"/echo/v1/echo/test": {
      "get": {
        "operationId": "test",
        "parameters": [
          {
            "name": "paramOne",
            "in": "query",
            "description": "First parameter test",
            "required": true,
            "type": "string",
          },
          {
            "name": "paramTwo",
            "in": "query",
            "description": "Second parameter test",
            "required": true,
            "type": "string",
          }
         ]

Here是一些有用的文档。在

编辑1:

一旦您在openapi.json文件,您需要调整Python代码以使用这些新参数。在

在这种情况下,您需要调整ResourceContainer以接受第二个参数。在

下面是一个例子:

^{pr2}$

需要记住的一点是:使用curl时,请记住在url中添加“”。不使用它可能会导致呼叫故障。在

谷歌论坛的这个答案为我解决了这个问题:

Are you using quotes around the URL in your curl command? Because if not, and you're in a bash shell (which is pretty likely if you're using curl), you might not actually be sending the &ends_at=THEEND part, because & is a shell metacharacter to put the command in the background.

编辑

链接到论坛中的答案 https://groups.google.com/forum/#!msg/google-cloud-endpoints/uzFcUS_uNz0/xyG2qVxbBQAJ

相关问题 更多 >

    热门问题