apiclient.discovery.build POST 请求正文

apiclient.discovery.build POST request body

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

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

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

POST 请求正文将需要包含一些字符串和几个列表,稍后可能包含一个 python 日期对象。

我的 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)

我自己想出来了。显然,您需要做的就是定义一个字典并将其作为服务方法调用中的 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()

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

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

这个有效