Google Endpoints GET request URL parameters [Python]

跟風遠走 提交于 2021-02-10 06:17:50

问题


I am currently working on a small project where I need to write a GET handler. I am working from the Echo example provided in the endpoints documentation.

I have my resource container:

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

And I have my handler:

@endpoints.method(
    GET_EMAIL_RESOURCE,
    EchoResponse,
    path='echo/getEmails/{i}',
    http_method='GET',
    name='echo_get_emails'
)
def echo_get_emails(self, request):
    if (request.i == 1):
        out = "default"
    else:
        out = "a"*request.i
    return EchoResponse(content=out)

And to access this I am using curl:

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

Now this works fine and returns what you expect, but if I need to encode more information into my URL such as:

getEmails/value1=someValue&value2=someOtherValue

I cannot figure out how to do this and I am unable to find an explanation or example in the docs.

Any help would be appreciated.

EDIT 1:

My code now looks like so:

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

and in my openapi.json under the path I have:

"/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"
      }
    ]
  }
}

Yet when I run the request curl --request GET --header "Content-Type: application/json" http://localhost:8080/_ah/api/echo/v1/echo/getEmails?trailingDigits=2&leadingDigits=2

I get back the contents of trailingDigits but the default for leadingDigits


回答1:


Any fields in a ResourceContainer not used in the path value should become query parameters.




回答2:


In order to modify parameters you need to configure the openapi.json file for the desired path. Here’s an example:

"/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's some useful documentation.

EDIT 1:

Once you edited the params in the openapi.json file, you need to adapt the Python code to use those new params.

In this case you’ll need to adapt the ResourceContainer to accept a second argument.

Here’s an example:

YOUR_RESOURCE_CONTAINER = endpoints.ResourceContainer(
message_types.VoidMessage,
parameter1=messages.IntegerField(1, default=1),
parameter2=messages.IntegerField(2, default=2))

One thing to keep in mind: when using curl remember to add "" to the url. Not using it may cause the call to malfunction.




回答3:


This answer from Google forums fixed the issue for me:

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.

Edit

Link to the answer in forum https://groups.google.com/forum/#!msg/google-cloud-endpoints/uzFcUS_uNz0/xyG2qVxbBQAJ



来源:https://stackoverflow.com/questions/49775802/google-endpoints-get-request-url-parameters-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!