Making calls from the Javascript client library wi

2019-04-13 17:35发布

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.

3条回答
手持菜刀,她持情操
2楼-- · 2019-04-13 18:10

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)
查看更多
forever°为你锁心
3楼-- · 2019-04-13 18:14

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!

查看更多
冷血范
4楼-- · 2019-04-13 18:25

The bug has been resolved. The correct syntax is

gapi.client.hylytit.hylyts.insert({url: url}, hylyt).execute(callback);
查看更多
登录 后发表回答