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

老子叫甜甜 提交于 2019-12-12 10:09:43

问题


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)


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/21021241/cloud-endpoints-resource-attribute-for-transmitting-named-params-body-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!