来自Mirror API的Google Glass callbackUrl POST为空?

时间:2013-07-18 17:28:19

标签: python google-mirror-api google-glass callbackurl

道歉,因为我所知道的唯一网络开发是django / python类型,我可能会混淆我的代码习惯(REST与django URL调度工作流程)

我有一个URL处理程序,它作为我的Glassware订阅的callbackUrl。我正在给处理程序一个POST,但请求对象似乎是空的。

我确信我理解这个错误,但有人可以指出我从POST通知到callbackURL获取“REPLY”信息的方向。

我的网址处理程序

class A600Handler(webapp2.RequestHandler):

def post(self):
    """Process the value of A600 received and return a plot"""
    # I am seeing this in my logs proving that I am getting a POST when glass replies
    logging.info("Received POST to logA600")
    # This is returning None
    my_collection = self.request.get("collection")
    logging.info(my_collection)
    # I also tried this but self.sequest.POST is empty '[]' and of type UnicodeMultiDict
    # json_request_data = json.loads(self.request.POST)


@util.auth_required
def get(self):
    """Process the value of A600 received and return a plot"""
    logging.info("Received GET to this logA600")

def post(self): """Process the value of A600 received and return a plot""" # I am seeing this in my logs proving that I am getting a POST when glass replies logging.info("Received POST to logA600") # This is returning None my_collection = self.request.get("collection") logging.info(my_collection) # I also tried this but self.sequest.POST is empty '[]' and of type UnicodeMultiDict # json_request_data = json.loads(self.request.POST) @util.auth_required def get(self): """Process the value of A600 received and return a plot""" logging.info("Received GET to this logA600")

我定义了以下URL Handler,并且当用户通过查看app-engine日志来回复时,可以验证post函数是否正在“ping”。

如何以用户发送的语音转录文本的形式提取有效负载?我不理解The "parse_notification" example given in the docs

2 个答案:

答案 0 :(得分:4)

你试过request.body吗? docs for request.POST

  

“如果您需要访问请求中发布的原始或非表单数据,请通过HttpRequest.body属性访问它。”

如果API未在其帖子中使用表单数据,您可能会在request.body中找到内容。 The docs to which you linked表示内容将作为JSON放置在正文中而不是表单数据(“包含JSON请求正文”)。我会尝试json.loads(request.body)

答案 1 :(得分:0)

我也遇到了这个Mirror API调用我的应用程序通知的问题,这些通知都是空的。我的应用程序在tomcat上运行,因此它是一个java堆栈。所有示例都按如下方式处理通知:

BufferedReader notificationReader = new BufferedReader(
                new InputStreamReader(request.getInputStream()));
        String notificationString = "";

        // Count the lines as a very basic way to prevent Denial of Service
        // attacks
        int lines = 0;
        while (notificationReader.ready()) {
            notificationString += notificationReader.readLine();
            lines++;

            // No notification would ever be this long. Something is very wrong.
            if (lines > 1000) {
                throw new IOException(
                        "Attempted to parse notification payload that was unexpectedly long.");
            }
        }

        log.info("got raw notification " + notificationString);

对我来说,这总是记录为空。由于通知URL必须是https,并且为了测试我无法使用IP地址,我设置dyndns服务指向我的localhost:8080运行服务。这一切似乎都有效,但我怀疑dyndns如何工作是某种类型的转发或重定向这里删除后期数据。

我如何解决这个问题以进行本地开发?

<强>更新: 解决了我。 我发现在读取请求导致request.inputStream已经关闭的问题之前关闭了响应。以此为准

response.setContentType("text/html");
Writer writer = response.getWriter();
writer.append("OK");
writer.close();

在我完全阅读请求通知到String后解决了问题。