Google Cloud Endpoints - Making calls using the JS client, passing parameters and JSON bodies

I am having trouble understanding some of the documents about this. Located in...

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

specially ...

// Insert a score gapi.client.tictactoe.scores.insert({'outcome': 'WON'}).execute(function(resp) { console.log(resp); }); // Get the list of previous scores gapi.client.tictactoe.scores.list().execute(function(resp) { console.log(resp); }); 

It looks like they pass the assessment object, as JSON in the request body, to their API call to add the assessment. Good, that sounds cool. However, it is unclear how you will pass the request parameter, the URL parameter, or all three of them at the same time.

Will they be such three JSON objects like this ...

 gapi.client.tictactoe.scores.insert({ 'outcome': 'WON' }, { urlParamName: 'value' }, { queryParamName: 'value' },).execute( ... 

Or are they all in the same JSON object? This can be very good, because Endpoints do not resolve name conflicts between parameters and members.

It seems that I can not find the documentation on this issue, can someone just help me so that I can know exactly in what format to transfer these things? Thanks.

+6
source share
1 answer

Unfortunately, this is not well documented, with the exception of some small references, for example. here

Usually API methods are called favorite:

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

params is a JSON object that includes all request parameters and paths, as well as resource , which will be sent as a body in a POST request.

Actually in the above example, they send the outcome as a request parameter as a POST request to tictactoe\v1\scores?outcome=WON with an empty body. This works because cloud endpoints do not affect the merging of the request object with the body and request parameters and URLs. This works in this case, but will not work as soon as you nest JSON objects inside the request body.

The correct way to call the method above:

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

If you have query parameters and URLs, it will look like this:

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

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

 gapi.client.myapi.myresource.mymethod({ 'param1': 'value1', 'param2': 'value2' }) 
+16
source

Source: https://habr.com/ru/post/957474/


All Articles