2013-07-18 20 views
2

道歉,因爲唯一的網絡發展中,我所知道的是Django的/ Python的種類,且很可能犯下混合我的代碼成語(REST VS Django的URL調度工作流程)Google Glass callbackUrl來自Mirror API的POST是空的?

的我有作爲callbackUrl到一個URL處理程序訂閱我的Glassware。我得到一個處理程序的POST,但請求對象似乎是空的。

我相信我理解這個錯誤,但有人可以指示我從POST通知到callbackURL獲取「REPLY」信息的方向。

我的URL處理器是

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") 

我有以下URL處理程序定義,可以驗證後功能得到一個「平」,當用戶通過查看應用程序引擎的日誌點擊回覆。

MAIN_ROUTES = [ ('/', MainHandler),('/logA600',A600Handler), ]

如何提取用戶發送的語音轉錄文本形式的有效載荷?我不理解The "parse_notification" example given in the docs

回答

4

你試過request.bodydocs for request.POST狀態

「如果您需要訪問請求中發佈的原始數據或非表單數據,請改爲通過HttpRequest.body屬性訪問此數據。」

如果該API未在其帖子中使用表單數據,則可能會在request.body中找到該內容。 The docs to which you linked表示內容將作爲JSON放置在主體中,而不是表單數據(「包含JSON請求主體」)。我會嘗試json.loads(request.body)

+0

感謝gotgenes,evrything確實隱藏在請求主體中。正如你從django處理它的方式中指出的那樣。 request.POST.get(「key」)僅適用於基於表單的POST信息。鏡像API返回都在我上面的處理程序類中的self.request.body中。 – harijay

0

我也有這個問題的鏡像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(); 

在我完全閱讀請求通知到一個字符串解決了問題。

+0

請注意,如果我使用Google的代理重定向服務以及我的IP地址硬編碼,我也會被調用,但仍然是空請求。 https://mirrornotifications.appspot.com/forward?url=http://1.2.3.4:8080/myapp-web/notify – bjm88

相關問題