我有一个小问题。我正在订阅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"
]
}
]
}
唯一的区别是参考字段: “reference”:“192.168.1.12:1028/accumulate?name=dupex”
我得到了:
{
"subscribeError": {
"errorCode": {
"code": "400",
"reasonPhrase": "Bad Request",
"details": "Illegal value for JSON field"
}
}
}
任何建议请:)谢谢。
答案 0 :(得分:2)
问题的根本原因是=
是一个禁止的字符,出于安全原因在有效负载请求中不允许(请参阅this section in the user manual有关它)。
有两种可能的解决方法:
http://192.168.1.12:1028/accumulate/name/dupex
。=
的代码为%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处的“订阅”和“自定义通知”部分。