cputoaster
cputoaster

Reputation: 644

cloud endpoints resource attribute for transmitting named params & body not working

I am trying to implement a call to a google cloud endpoint via the gapi.client rpc calls. As described in the docs (and Google Cloud Endpoints - Making calls with JS client, passing params and JSON body), it should be possible to include named parameters and an object body:

@ApiMethod(name = "staff.insert", httpMethod="post")
    public Staff insertStaff(@Named("token") String token, Staff staff) throws ConflictException, NotFoundException, InternalServerErrorException {

should be callable via

var staff = {};
           staff.id = environment.getStaffId();
           staff.name = "Johnny";
           staff.createdAt = new Date();
           staff.modifiedAt = new Date();

           var par = { 'token' : "mytoken", 'resource' : staff};

           api.staff.insert(par).execute(function(res) {

           });

I cannot get this to work, I always get an empty staff object in the java endpoint, but a correct token. This is on dev env (1.8.8) and on appengine. gapi.client version is 1.1.0-beta.

The endpoint works fine with correct staff being transmitted when used via api explorer (which uses rest instead of rpc) and android generated endpoint libraries (also using json rest).

Dumping the rpc request in the chrome debugger shows that there is a params dictionary that has the resource dictionary and the token params inside, it does not look like the resource attribute is handled specially by gapi.client. Is this how it should be, and the endpoints code has special handlers for "resource" params? or is this a gapi problem?

Using the chrome debugger on the gapi.client obfuscated source and setting a breakpoint on the only string occurance of "resource" I could find, the breakpoint is never hit. Am I doing something wrong with formatting the params / body? Any ideas? Or questions I could clarify about my setup?

(I also posted the question on the gapi.client discussion group, but its not clear if the problem is in gapi or in endpoints)

Upvotes: 2

Views: 300

Answers (2)

da Bich
da Bich

Reputation: 536

Yup, agreed it's a bug. caused me great pains as well.

So i guess the work around is to create a combined object to pass to your api all named and un named parameters. Rather than hardcode each.. a quick loop might be better.

var param = {};
param["token"] = "mytoken";
for (var prop in staff) {
  param[prop] = staff[prop];
}
api.staff.insert(param).execute(function(res) {
       }); 

That mashing together of parameters / objects can become a slick function if you really want.. but it's a band aid for what I'd consider a defect.

Upvotes: 1

willlma
willlma

Reputation: 7543

It makes no sense, but try this:

var date = new Date();
var par = {
    'token': 'mytoken',
    'id': environment.getStaffId(),
    'name': "Johnny",
    'createdAt': date,
    'modifiedAt': date
}

I've complained about this in the past.

Upvotes: 1

Related Questions