Issue in applying routing query commands in IoT Hub Azure Portal

Athira Gopinath(UST,IN) 141 Reputation points
2023-04-04T07:09:39.5533333+00:00

Hi, As part of my research, I want to filter out the data coming from IoT Hub based on certain conditions. For that purpose, I have tried out ""IoT Hub message routing query syntax" published by Azure(https://learn.microsoft.com/en-us/azure/iot-hub/iot-hub-devguide-routing-query-syntax) by giving different routing queries specified in the document. It is observed that, the routing queries are not working as expected. Please suggest a solution to solve this problem.

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,272 questions
{count} votes

Accepted answer
  1. Sander van de Velde | MVP 36,766 Reputation points MVP Volunteer Moderator
    2023-04-04T21:23:10.1066667+00:00

    Hello @Athira Gopinath(UST,IN), the IoT Hub supports routing messages to several kinds of endpoints based on routing rules. These rules can be filtered to filter incoming messages. Because the message body could be anything, it is passed as a byte array, the regular routing ignores the message body. In most cases, Azure resources assume that the body contains a JSON message but that is not the case this time. As @Matthijs van der Veer mentioned already you need to provide some extra content information

    using (var message = new Message(messageBytes)) 
    { 
        // Set message body type and content encoding. 
        message.ContentEncoding = "utf-8"; 
        message.ContentType = "application/json"; 
     
        await deviceClient.SendEventAsync(message); 
    }
    

    This is the case with all IoT Devices SDKs (like C#, Javascript, Python) and even the non-SDK MQTT way of communication. Now, the IoT Hub knows the incoming byte array message can safely be decoded into JSON so the body properties can be read. If the response helped, do "Accept Answer". If it doesn't work, please let us know the progress. All community members with similar issues will benefit by doing so. Your contribution is highly appreciated.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.