Mic*_*els 2 python google-app-engine http-post google-api-client
我的服务器 Google App Engine API 代码和我的客户端都使用 python。我已经有很多客户端-服务器 GET 请求在工作,现在是 POST 请求的时候了,但我被卡住了。我的相关客户代码:
service = build("myAPI", "v1", http=http, discoveryServiceUrl=DISCOVERY_URL)
service.updateUser(websafeKey=websafeKey).execute()
Run Code Online (Sandbox Code Playgroud)
我不知道如何构建 POST 请求的正文。在那之后,我将被困在试图弄清楚如何告诉service它以便它可以将请求发送到我的 updateUser API。
POST 请求正文将需要包含一些字符串和几个列表,然后可能包含一个 python Date 对象。
我的updateUserAPI 在 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)
Run Code Online (Sandbox Code Playgroud)
我自己想通了。显然,您需要做的就是定义一个字典并将其作为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()
Run Code Online (Sandbox Code Playgroud)
非常简单,真的,但我确实很难找到任何记录这一点的东西。希望其他人可以受益。
| 归档时间: |
|
| 查看次数: |
1054 次 |
| 最近记录: |