Reputation: 1345
I am processing a message from an EventHub connected with an IoT Hub. I will try to explain the whole process.
Using a terminal, I send the following command to an IoT hub component deployed on Azure:
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
When the Azure function receive the event (curl -> IoT hub -> Event-hub <- Azure Function) and print the content:
@FunctionName("processSensorData")
public void processSensorData(
@EventHubTrigger(
name = "demo",
eventHubName = "", // blank because the value is included in the connection string
cardinality = Cardinality.ONE,
connection = "EventHubConnectionString")
String item,
final ExecutionContext context) {
context.getLogger().info("Event hub message received: " + item.toString());
I receive the following message in the console:
[
{
"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"
}]
but the body appear to be encrypted.
How to decode the body to return the original request?
"{ \"field1\" : \"12345\", \"field2\" : \"abcde\" }"
Many thanks in advance
Juan Antonio
Upvotes: 0
Views: 798
Reputation: 8235
You should setup the system message properties such as the contentType to application/json and contentEncoding to UTF-8 in your POST request.
Upvotes: 1
Reputation: 2034
Body is base64 URL encoded. You should decode it in your code and then parse the JSON object out. Try this code for decoding:
var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
var jsonStr = System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
Upvotes: 0