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

前端 未结 1 1773
渐次进展
渐次进展 2021-02-04 18:23

I am having some trouble understanding some documentation on this. Located at ...

https://developers.google.com/appengine/docs/java/endpoints/consume_js

specific

相关标签:
1条回答
  • 2021-02-04 18:35

    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'
    })
    
    0 讨论(0)
提交回复
热议问题