Making calls from the Javascript client library with @Named and unnamed parameters makes no sense

邮差的信 提交于 2019-12-06 10:44:42

问题


I have a Cloud Endpoints method that looks like this:

//HTTP POST
@ApiMethod(name = "hylyts.insert")
public Hylyt insertHylyt(@Named("url") String url, Hylyt hylyt, User user)
        throws OAuthRequestException{
    log.info("Trying to save hylyt '"+hylyt+"' with id '"+hylyt.getId());
    if (user== null) throw new OAuthRequestException("Your token is no good here.");
    hylyt.setArticle(getArticleKey(url, user));
    ofy().save().entity(hylyt);
    return hylyt;
}

I call it from the Javascript Client Library using this:

gapi.client.hylytit.hylyts.insert({PARAMS}).execute(callback);

Now, if I structure {PARAMS} as suggested in the docs (second example),

{
  'url': url,
  'resource': {
    'hylyt': {
      'contentType': 'application/json',
      'data': hylyt
    }
  }
}

I get a null object in the endpoint (not to mention that the whole point of this library is to make these calls simple, which this structure clearly violates).

When I structure {PARAMS} as these answers suggest,

{
  'url': url,
  'resource': hylyt
}

I get a null object in the endpoint again. The correct syntax is this:

{
  'url': url,
  'id': hylyt.id
  'text': hylyt.text
}

Which just blows my mind. Am I doing this all wrong? Is this a bug? Is it only happening because gapi is also passing the auth token in the background?

Yes, I could use the request syntax instead, but, again, why even use the library if it's just as complex as making the XHRs in pure javascript? I wouldn't mind the complexity if Google explained in the docs why things are happening. But the docs, paraphrased, just say use these methods and the auth, CORS, and XHR magic will happen behind closed doors.


回答1:


Is the API method correctly recognized as POST method?

The resource parameter which is sent as POST body won't work correctly in a GET request. The way it looks you are actually sending a GET request with the Hylyt properties in the query string.

To make sure you can change the method annotation to this:

@ApiMethod(name = "hylyts.insert", httpMethod = HttpMethod.POST)



回答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["url"] = url;
for (var prop in hylyt) {
  param[prop] = hylyt[prop];
}
gapi.client.hylytit.hylyts.insert(param).execute(callback);

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. I see in the related question (cloud endpoints resource attribute for transmitting named params & body not working), you actually logged a defect.. Good stuff. Though there still appears no movement on this one. fingers crossed for someday!




回答3:


The bug has been resolved. The correct syntax is

gapi.client.hylytit.hylyts.insert({url: url}, hylyt).execute(callback);


来源:https://stackoverflow.com/questions/19988725/making-calls-from-the-javascript-client-library-with-named-and-unnamed-paramete

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