Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Content-Type alternative in MQTT

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

like image 804
Shak Avatar asked Jan 28 '26 22:01

Shak


2 Answers

2021 answer

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.

sources

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

like image 143
Cristiano Avatar answered Jan 31 '26 19:01

Cristiano


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

like image 21
hardillb Avatar answered Jan 31 '26 18:01

hardillb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!