2015-06-16 30 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

相关问题