Mason Mason - 7 months ago 20
Javascript Question

Cloud Endpoint parameter should not be named

I want send a

HashMap<String, String>
from JS application to my Google App. I created a
HashMapContainer
class such as in : Cloud Endpoints Collection Parameter.


The Endpoint method is defined like this :

public Entity myMethod(
@Named('param1') String param1,
@Nullable @Named('param2') HashMapContainer param2) {
//...
}


When I run the API generation this error happens :

com.google.api.server.spi.config.validation.ApiConfigInvalidException:
Resource type 'class com.mason.server.entity.HashMapContainer'
in method 'endpoint.myMethod' should not be named.


Therefore, I remove the
@Named
annotation. The API is generated but obviously, I do not receive the parameter send by the JS application. My JavaScript is like this :

function doTransaction() {
var req = gapi.client.myApi.endpoint.myMethod({
'param1': 'FOO',
'param2': {
'value1':'foofoo',
'value2':'barbar',
'value3':'foobar'
}
});
req.execute(function(data) {
console.log(data);
});
}


How I can get the
param2
if I'm not allowed to use the
@Named
annotation ?

Maybe my JavaScript is wrong?

Answer

Google Cloud Enpoints documentation says:

@Named: This annotation indicates the name of the parameter in the request that gets injected here. A parameter that is not annotated with @Named is injected with the whole request object.

Basically, as far as I understand, when you add @Named annotation, the parameters will be included at the end of the request URL:

http://end_point_url?parameter1=xxx&parameter2=yyy

Obviously the parameter types that support @Named annotation are only a few (int, long, String, Boolean and their correspondent arrays, I think), because you can't append a whole hashmap to the request URL!

On the other hand, if you don't use @Named, the parameter will be included (injected) within the POST data.

In order to send a parameter within the HTTP body using the Google APIs Client Library for JavaScript, you just have to include that parameter into an object called resource inside the JSON-RPC request, like this:

var req = gapi.client.myApi.endpoint.myMethod({
    'param1': 'FOO',
    'resource': {
        'param2': {
            'value1':'foofoo',
            'value2':'barbar',
            'value3':'foobar'
        }
    }
});

The API client will automatically send param1 in the URL and param2 in the POST data...

This is explained in detail in this section of the Google APIs Client Library for JavaScript documentation.