microsoft / vscode-azure-iot-toolkit

Azure IoT Hub extension for Visual Studio Code
https://marketplace.visualstudio.com/items?itemName=vsciot-vscode.azure-iot-toolkit
Other
73 stars 58 forks source link

D2C message interface should support JSON contentType #440

Closed SlothFriend closed 4 years ago

SlothFriend commented 4 years ago

Please allow support for different contentTypes when submitting D2C messages, specifically JSON type. This is a significant problem because IoT Hub route queries only work when contentType is set to application/json. This means I can't use the extension to test routes and their down-stream connections.

Screen Shot 2019-12-20 at 3 25 21 PM
SlothFriend commented 4 years ago

Thank you for your work and your time considering this new feature.

formulahendry commented 4 years ago

Hi @SlothFriend , if you input a valid json, the D2C message will be sent as what it is. image Is this JSON message different with what you do with other way (e.g. using device SDK)?

formulahendry commented 4 years ago

Closing due to inactivity. Please reopen this issue or create a new issue if you need more help.

SlothFriend commented 4 years ago

Hi @formulahendry!

Sorry for the delay - I've been out of office with the holidays. To continue the conversation...

While the body arrives as a string in JSON format, the main issue lies with the contentType not being set to JSON. When sending messages with the SDK, we would do something like this:

const message = new Message(data);
message.contentEncoding = "utf-8";
message.contentType = "application/json";

It is my experience (and I've read from a few sources) that without an explicit set of this contentType message routing in IoT Hub based on message body properties does not work. This is what I'm experiencing when sending D2C messages through VSCode.

For reference, here's an article mentioning the requirement on contentType. Hope it helps. https://azure.microsoft.com/en-us/blog/iot-hub-message-routing-now-with-routing-on-message-body/

Screen Shot 2020-01-06 at 11 31 35 AM

Let me know if this makes sense. Thanks!

formulahendry commented 4 years ago

Thanks @SlothFriend for the detailed info! We will investigate on it

pdesjardins90 commented 4 years ago

I'm having the same problem on my end. My code expects to be able to read a json-formatted body from the IoT hub event but instead it's being encoded in base64 as stated here:

Device telemetry message must be in a valid JSON format with the contentType set to application/json and contentEncoding set to UTF-8 in the message system properties. Both of these properties are case insensitive. If the content encoding is not set, then IoT Hub will write the messages in base 64 encoded format.

Took me a while to figure this out, this behavior is not intuitive at all

michielvanschaik commented 4 years ago

Same here. Testing IoT Hub routing did not work when generating messages using the D2C message interface from this extension. The IoTHub log shows the error "Content encoding is not specified in system properties." Please add the ContentEncoding and ContentType system properties as suggested by @SlothFriend.

montgomp commented 4 years ago

Thank you Michiel, this will be part of release 2.16.3. Closing this issue.

rarnken commented 3 years ago

@montgomp for me sending D2C message from this extension is still not working (version 2.16.6). The resulting data in body is still somehow encoded. Can you share a sample for plain text and/or data template message with which should be used to test routing in IotHub?

dushdushyant commented 9 months ago

This has been incorporated in 2.4.1 the Message class has an added setter for set encoding