Google Cloud Endpoints-使用JS客户端进行调用,传递参数和JSON正文 [英] Google Cloud Endpoints - Making calls with JS client, passing params and JSON body

查看:85
本文介绍了Google Cloud Endpoints-使用JS客户端进行调用,传递参数和JSON正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在理解有关此文档的过程中遇到了一些麻烦.位于...

I am having some trouble understanding some documentation on this. Located at ...

https://developers.google.com/appengine/docs/java/端点/consume_js

特别是...

// Insert a score
gapi.client.tictactoe.scores.insert({'outcome':
    'WON'}).execute(function(resp) {
  console.log(resp);
});

// Get the list of previous scores
gapi.client.tictactoe.scores.list().execute(function(resp) {
  console.log(resp);
});

好像他们正在将评分对象(如请求正文中的JSON)传递给其API调用以添加评分.好吧,听起来很酷.不过,目前尚不清楚如何传递查询参数,URL参数,或者同时传递三个参数.

Seems like they are passing a score object, as JSON in the request body, to their API call for adding a score. Okay, sounds cool. It's unclear though on how you would pass a query parameter, URL parameter, or may be all three at the same time.

它们将是三个这样的JSON对象...

Would they be three JSON objects like this ...

gapi.client.tictactoe.scores.insert({
    'outcome': 'WON'
},
{
    urlParamName: 'value'
},
{
    queryParamName: 'value'
},).execute( ...

还是它们都在同一个JSON对象中?事实很可能是这样,因为端点不允许参数和成员之间发生任何名称冲突.

Or are they all in the same JSON object? That may very well be the case since Endpoints dosn't allow any name conflicts between params and members.

我似乎找不到关于此的文档,有人可以简单地帮助我吗,这样我才能确定传递这些内容的格式是什么?谢谢.

I can't seem to find documentation on this, could someone simply help me out so I can know for sure what's the format to pass these things? Thanks.

推荐答案

不幸的是,除了一些小小的提及,

Unfortunately this isn't very well documented, except for some small mentions, e.g. here

通常这样称呼API方法:

Usually API methods are called liked this:

gapi.client.myapi.myresource.mymethod(params)

params是一个JSON对象,其中包括所有查询和路径参数,以及一个resource,它将在POST请求中作为正文发送.

params is a JSON object that includes all query and path parameters, as well as a resource which would be send as body in a POST request.

实际上,在上面的示例中,他们将outcome作为查询参数作为POST请求发送给具有空主体的tictactoe\v1\scores?outcome=WON.这是可行的,因为Cloud Endpoints不会合并正文和查询以及URL参数中的请求对象.在这种情况下,此方法有效,但是一旦在请求正文中嵌套了JSON对象,该操作就会失败.

Actually in the example above they are sending outcome as a query parameter as a POST request to tictactoe\v1\scores?outcome=WON with an empty body. This works since Cloud Endpoints don't make a difference merging the request object from the body and query and URL parameters. This works in this case but will fail as soon as you have nested JSON Objects inside of the request body.

调用上述方法的正确方法是

The correct way to call above method would be

gapi.client.tictactoe.scores.insert({
    'resource': {'outcome': 'WON'}
})

如果您有查询和URL参数,则如下所示:

If you have query and URL parameters this would look like this:

gapi.client.myapi.myresource.mymethod({
    'param1': 'value1',
    'param2': 'value2',
    'resource': body
})

,其中body可以是任何JSON对象.或是不需要身体的方法

where body could be any JSON object. Or for methods that don't need a body you would just have

gapi.client.myapi.myresource.mymethod({
    'param1': 'value1',
    'param2': 'value2'
})

这篇关于Google Cloud Endpoints-使用JS客户端进行调用,传递参数和JSON正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆