【问题标题】:apiclient.discovery.build POST request bodyapiclient.discovery.build POST 请求正文
【发布时间】:2015-12-24 23:11:17
【问题描述】:

我将 python 用于我的服务器 Google App Engine API 代码和我的客户端。我已经有很多客户端-服务器 GET 请求在工作,现在是 POST 请求的时候了,我被困住了。我的相关客户代码:

service = build("myAPI", "v1", http=http, discoveryServiceUrl=DISCOVERY_URL)
service.updateUser(websafeKey=websafeKey).execute()

我不知道如何构建 POST 请求的正文。在那之后,我将被困在试图弄清楚如何告诉 service 以便它可以将请求发送到我的 updateUser API。

POST 请求正文需要包含一些字符串和几个列表,之后可能还需要一个 python Date 对象。

我的updateUser API 在 API Explorer 中运行良好 - 我可以整天成功地updateUser

USER_POST_REQUEST = endpoints.ResourceContainer(
UserForm, websafeKey=messages.StringField(1),)
@endpoints.method(USER_POST_REQUEST, UserForm,
        path='useradmin/{websafeKey}',
        http_method='PUT', name='updateUser')
def updateUser(self, request):
    """Update user w/provided fields & return w/updated info."""
    return self._updateUserObject(request)

【问题讨论】:

    标签: python google-app-engine http-post google-api-client


    【解决方案1】:

    我自己想通了。显然,您需要做的就是定义一个字典并将其作为body 参数传递给服务方法调用。这是我所做的:

    body = {'key1': user['info1'], 'key2': user['info2']}
    service = build("myAPI", "v1", http=http, discoveryServiceUrl=DISCOVERY_URL)
    service.updateFan(websafeFanKey=fan['websafeKey'], body=body).execute()
    

    非常简单,真的,但我确实很难找到任何记录这一点的东西。希望其他人可以受益。

    【讨论】:

      【解决方案2】:
      body = {"messages":"XXX"}
      
      pubsubpublisher = googleapiclient.discovery.build('pubsub', 'v1', credentials=credentials)
      
      response = pubsubpublisher.projects().topics().publish(topic='projects/{project-number}/topics/{topic-name}',body=body).execute()
      

      这行得通

      【讨论】:

      • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
      猜你喜欢
      • 1970-01-01
      • 2018-07-18
      • 2019-05-11
      • 2016-04-11
      • 2017-12-01
      • 2021-12-05
      • 1970-01-01
      • 2018-06-16
      • 2019-03-13
      相关资源
      最近更新 更多