2015-06-16 32 views
1

我有一個小問題。林做了訂閱獵戶座語境經紀人,我有一個奇怪的問題,回調的網址: 此代碼教程作品:Orion Context Broker - 訂閱

{ 
    "entities": [ 
     { 
      "type": "Room", 
      "isPattern": "false", 
      "id": "Room1" 
     } 
    ], 
    "attributes": [ 
     "temperature" 
    ], 
    "reference": "http://localhost:1028/accumulate", 
    "duration": "P1M", 
    "notifyConditions": [ 
     { 
      "type": "ONTIMEINTERVAL", 
      "condValues": [ 
       "PT10S" 
      ] 
     } 
    ] 
} 

但這個代碼不工作:

{ 
    "entities": [ 
     { 
      "type": "Room", 
      "isPattern": "false", 
      "id": "Room1" 
     } 
    ], 
    "attributes": [ 
     "temperature" 
    ], 
    "reference": "http://192.168.1.12:1028/accumulate?name=dupex", 
    "duration": "P1M", 
    "notifyConditions": [ 
     { 
      "type": "ONTIMEINTERVAL", 
      "condValues": [ 
       "PT10S" 
      ] 
     } 
    ] 
} 

唯一的區別是參考現場: 「參考」: 「192.168.1.12:1028/accumulate?name=dupex」

我:

{ 
    "subscribeError": { 
     "errorCode": { 
      "code": "400", 
      "reasonPhrase": "Bad Request", 
      "details": "Illegal value for JSON field" 
     } 
    } 
} 

任何建議請:)謝謝。

回答

2

問題的根本原因是=是一個禁止的字符,出於安全原因不允許在有效負載請求中(請參閱this section in the user manual瞭解它)。

有兩種可能的解決方法:

  1. 避免查詢字符串的URL中的使用在subscribeContext,例如參考使用http://192.168.1.12:1028/accumulate/name/dupex
  2. 編碼URL encoding以避免禁止的字符(特別是,=的代碼是%3D)並準備好您的代碼以對其進行解碼。

在情況2中,您可以使用subscribeContext中的follwoing引用:http://192.168.1.12:1028/accumulate?name%3Ddupex。然後,將考慮編碼和正確得到name參數的代碼的例子是以下(使用燒瓶REST服務器框架用Python寫的):

from flask import Flask, request 
from urllib import unquote 
from urlparse import urlparse, parse_qs 
app = Flask(__name__) 

@app.route("/accumulate") 
def test(): 
    s = unquote(request.full_path) # /accumulate?name%3Dduplex -> /accumulate?name=duplex 
    p = urlparse(s)    # extract the query part: '?name=duplex' 
    d = parse_qs(p.query)   # stores the query part in a Python dictionary for easy access 
    name= d['name'][0]    # name <- 'duplex' 
    # Do whatever you need with the name... 
    return "" 

if __name__ == "__main__": 
    app.run() 

我想,類似的方法可以用於其他語言(Java,Node等)。

編輯:獵戶版本1.2支持NGSIv2中的通知定製,它允許使用這種用例。例如,您可以定義如下訂閱:

{ 
    .. 
    "notification": { 
    "httpCustom": { 
     "url": "http://192.168.1.12:1028/accumulate", 
     "qs": { 
     "name": "dupex" 
     } 
    } 
    .. 
    } 
    .. 
} 

請看看以「訂閱」和「自定義通知」部分在NGSIv2 Specification瞭解詳情。

+0

0123在回答中描述的變通辦法中,我們已將此作爲Orion存儲庫中的問題,以考慮問題並最終提供更好的解決方案:https://github.com/telefonicaid/fiware-orion /問題/ 1015。對這個問題的評論值得歡迎! – fgalan

+0

Orion 1.2(將於2016年6月初發布)將啓用該用例。有關它的信息已被添加到答案文中(查看「編輯」)。 – fgalan

相關問題