I am Working on MQTT communication using Paho and Mosqitto. We have to support both model of serialization - xml and json. So I am looking How to identify the content type or payload type in MQTT. Is there something similar HTTP Content-Type in MQTT to identify it quickly ?
Content-Type : application/json
Content-Type : application/xml
Thanks
MQTT 5.0 introduced the concept of Properties. Basically, properties are UTF-8 string key-value pairs that you can add to an MQTT packet. The new specification also defines payload-format and content-type to convey information about the MIME type contained in the payload. So in principle, you can use this property in your application much like in HTTP you use Content-Type header.
payload-format: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901111 content-type: https://docs.oasis-open.org/mqtt/mqtt/v5.0/os/mqtt-v5.0-os.html#_Toc3901118
No, MQTT payloads are just byte arrays and there is no space in the headers (because MQTT is designed to be as light weight as possible on the network). Anything else is down to the application to implement with in the payload.
You could use multiple topics to show the difference.
e.g. foo/bar/xml or foo/bar/json and subscribe to foo/bar/+ which will match both and then switch based on the topic.
or just test the first char of the payload, '{' = json '<' = xml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With