【问题标题】:Getting parameter from request path in Google Cloud Endpoints从 Google Cloud Endpoints 中的请求路径获取参数
【发布时间】:2015-02-02 15:14:34
【问题描述】:

我正在 App Engine 上使用 Python 中的 Cloud Endpoint API 进行试验,但在获取简单的请求参数时遇到了一些困难。

我更熟悉 Java 的 Cloud Endpoints,所以我可能(可能)遗漏了一些明显的东西。在此示例中,我要做的只是返回带有在 url 路径中指定的 id 的 ServiceInfo 对象,/services/<id>

我有一个简单的响应消息类:

class ServiceInfo(messages.Message):
    crs = messages.StringField(1)
    services = messages.StringField(2)

和 API 类:

@endpoints.api(name='myApi', version='v1', description='My API', audiences=[endpoints.API_EXPLORER_CLIENT_ID])
class MyApi(remote.Service):

    #No request body, but need to capture the id from the URL
    ID_RESOURCE = endpoints.ResourceContainer(
        message_types.VoidMessage,
        id=messages.StringField(1, variant=messages.Variant.STRING, required=True))

    @endpoints.method(ID_RESOURCE, ServiceInfo, path='services/{id}', http_method='GET', name='station.getServices')
    def get_services(self, request):
        print request.id
        ...
        return ServiceInfo(crs=request.id, services=...)

现在,如果我通过 API 资源管理器发出请求并输入 ABC 作为 id 字段,我会看到此请求:

GET /_ah/api/myApi/v1/services/ABC

但是回复说

"Error parsing ProtoRPC request (Unable to parse request content: Message CombinedContainer is missing required field id)"

当我打印request.id 时,我得到None

我要做的就是从路径中获取id - 我是否遗漏了一些非常明显的东西?

谢谢!

【问题讨论】:

  • 它是否适用于以services?id=ABC 而不是services/ABC 结尾的路径?文档似乎说它们都可以工作,但我会先尝试一下 - 然后我会尝试将 arg 从 id 重命名为 foo 以防万一您遇到一些未提及的问题“保留名称”。让我们知道...!
  • 有趣! services?id=ABC 给出 404 但services/?id=ABC 给了我正确的行为。我可以说id 似乎也没有什么特别之处。所以现在我只需要弄清楚为什么它在 url 路径中不起作用。我似乎没有做任何与this 示例不同的事情。
  • 您是否可以链接显示此行为的已部署应用程序?
  • 这实际上只是在本地运行,但在关闭这个项目几天后,我重新启动了本地开发服务器,它似乎正在工作,没有代码更改。我会将问题标记为已回答。

标签: python google-app-engine google-cloud-endpoints


【解决方案1】:

几天后重新访问后,我重新启动了本地开发服务器(使用gcloud preview app run ...),现在它似乎正在使用url路径中的id(没有代码更改)所以也许开发服务器环境一直在缓存我的一个文件的旧版本?

【讨论】:

    猜你喜欢
    • 2014-07-10
    • 2021-08-02
    • 2017-01-07
    • 2021-08-22
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    • 2013-05-30
    • 1970-01-01
    相关资源
    最近更新 更多