Google Cloud Endpoints - Making calls with JS client, passing params and JSON body

点点圈 提交于 2019-12-03 00:33:15

Unfortunately this isn't very well documented, except for some small mentions, e.g. here

Usually API methods are called liked this:

gapi.client.myapi.myresource.mymethod(params)

params is a JSON object that includes all query and path parameters, as well as a resource which would be send as body in a POST request.

Actually in the example above they are sending outcome as a query parameter as a POST request to tictactoe\v1\scores?outcome=WON with an empty body. This works since Cloud Endpoints don't make a difference merging the request object from the body and query and URL parameters. This works in this case but will fail as soon as you have nested JSON Objects inside of the request body.

The correct way to call above method would be

gapi.client.tictactoe.scores.insert({
    'resource': {'outcome': 'WON'}
})

If you have query and URL parameters this would look like this:

gapi.client.myapi.myresource.mymethod({
    'param1': 'value1',
    'param2': 'value2',
    'resource': body
})

where body could be any JSON object. Or for methods that don't need a body you would just have

gapi.client.myapi.myresource.mymethod({
    'param1': 'value1',
    'param2': 'value2'
})
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!