Azure functions 如何从IoT中心反序列化传递给事件中心的事件?
我正在处理来自与物联网中心连接的事件中心的消息。我会尽力解释整个过程 使用终端,我向部署在Azure上的IoT hub组件发送以下命令:Azure functions 如何从IoT中心反序列化传递给事件中心的事件?,azure-functions,azure-iot-hub,azure-eventhub,Azure Functions,Azure Iot Hub,Azure Eventhub,我正在处理来自与物联网中心连接的事件中心的消息。我会尽力解释整个过程 使用终端,我向部署在Azure上的IoT hub组件发送以下命令: curl --request POST \ --url "https://${IOT_HUB}.azure-devices.net/devices/${DEVICE}/messages/events?api-version=2018-06-30" \ --header "Accept: application/json" \ --h
curl --request POST \
--url "https://${IOT_HUB}.azure-devices.net/devices/${DEVICE}/messages/events?api-version=2018-06-30" \
--header "Accept: application/json" \
--header "Authorization: ${SAS_TOKEN}" \
--data "{ \"field1\" : \"12345\", \"field2\" : \"abcde\" }" \
--verbose
Azure函数接收事件时(curl->IoT hub->event hubBody是base64 URL编码的。您应该在代码中对其进行解码,然后解析JSON对象。请尝试以下代码进行解码:
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
var jsonStr = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
您应该在POST请求中设置系统消息属性,例如contentType到application/json和contentEncoding到UTF-8。我注意到发送该标题时,正文的内容显示为清晰。Azure为什么会有这种行为?在对内容(正文)使用json编码的情况下,则消息必须设置contentType和contentEncoding系统属性,否则正文为base64编码格式。
[
{
"id":"xxx",
"topic":"/SUBSCRIPTIONS/xxx/RESOURCEGROUPS/xxxPROVIDERS/MICROSOFT.DEVICES/IOTHUBS/Txxxx",
"subject":"devices/xxx",
"eventType":"Microsoft.Devices.DeviceTelemetry",
"eventTime":"2020-04-13T15:02:15.253Z",
"data":{
"properties":{
},
"systemProperties":{
"iothub-content-type":"application/x-www-form-urlencoded",
"iothub-content-encoding":"",
"iothub-connection-device-id":"xxx",
"iothub-connection-auth-method":"{\"scope\":\"device\",\"type\":\"sas\",\"issuer\":\"iothub\",\"acceptingIpFilterRule\":null}",
"iothub-connection-auth-generation-id":"xxx",
"iothub-enqueuedtime":"2020-04-13T15:02:15.253Z",
"iothub-message-source":"Telemetry"
},
"body":"yyy"
},
"dataVersion":"",
"metadataVersion":"1"
}]
"{ \"field1\" : \"12345\", \"field2\" : \"abcde\" }"
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
var jsonStr = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);