Search code examples
pythongoogle-app-enginehttp-postgoogle-api-client

apiclient.discovery.build POST request body


I am using python for both my server Google App Engine API code and my client. I have plenty of client-server GET requests working already, now it's time for a POST request and I'm stuck. My relevant client code:

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

I can't figure out how to build the body of the POST request. After that I will be stuck trying to figure out how to tell service about it so that it can send the request to my updateUser API.

The POST request body will need to contain some strings and several lists, and later possibly a python Date object.

My updateUser API works fine in the API Explorer - I can successfully updateUser all day long:

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)

Solution

  • I figured it out myself. Apparently all you need to do is define a dictionary and pass it as the body parameter in the service method call. Here's what I did:

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

    Pretty simple, really, but it sure was difficult for me to find anything that documented this. Hopefully someone else can benefit.