【问题标题】:Orion Context Broker - subscriptionsOrion 上下文代理 - 订阅
【发布时间】:2015-06-16 15:15:32
【问题描述】:

我有一个小问题。我正在订阅 Orion Context Broker,但我对回调 URL 有一个奇怪的问题: 此代码适用于教程:

{
   "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"
        }
    }
}

任何建议请:)谢谢。

【问题讨论】:

    标签: fiware fiware-orion


    【解决方案1】:

    问题的根本原因是=是一个禁止字符,出于安全原因不允许在payload请求中使用(参见this section in the user manual)。

    有两种可能的解决方法:

    1. 避免在 URL 中使用查询字符串以在 subscribeContext 中引用,例如使用http://192.168.1.12:1028/accumulate/name/dupex
    2. 编码URL encoding 以避免禁用字符(特别是= 的代码是%3D)并准备好您的代码来解码它。

    在情况 2 中,您可以在 subscribeContext 中使用以下引用:http://192.168.1.12:1028/accumulate?name%3Ddupex。然后,将考虑编码并正确获取 name 参数的代码示例如下(使用 Flask 作为 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 等)也可以使用类似的方法。

    编辑:Orion 1.2 版支持 NGSIv2 中的通知自定义,从而允许此用例。例如,您可以定义以下订阅:

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

    请查看NGSIv2 Specification 的“订阅”和“自定义通知”部分了解详细信息。

    【讨论】:

    • 答案中描述的解决方法的一部分,我们已将此作为问题包含在 Orion 存储库中,以便对其进行思考并最终提供更好的解决方案:github.com/telefonicaid/fiware-orion/issues/1015。欢迎对问题发表评论!
    • Orion 1.2(将于 2016 年 6 月上旬发布)将启用该用例。有关它的信息已添加到答案帖子中(在“编辑”下查看)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    相关资源
    最近更新 更多