Issue in adding message properties in paho MQTT publish method

Athira Gopinath(UST,IN) 121 Reputation points
2023-04-11T10:21:35.9+00:00

Hi, As part of our work, we need to apply routing queries to the payload receives at IoTHub via MQTT protocol. For that purpose, the properties of payload should be as {'content-encoding': 'utf-8','content_type':'application/json'}. So that we have applied these properties to this payload Payload:{"datetime": "2023-03-30 06:20:30","RaR": 123, "level": 750, "id": "1234"}. Attaching the code snippet.

final_payload={"datetime": "2023-03-30 06:20:30","RaR": 123, "level": 750, "id": "1234"}
publish_properties = props.Properties(PacketTypes.CONNECT)
publish_properties.UserProperty = ("application/json","3")
publish_properties.UserProperty = ("content_encoding", "UTF-8")
client.publish(topic, payload=final_payload, qos=1,properties=publish_properties)

But the properties are not getting applied on the payload. It is not getting filtered out. Please suggest some solution to resolve this issue. Thanks Athira

Azure IoT Hub
Azure IoT Hub

An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.

0 comments No comments

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    5 deleted comments

    Comments have been turned off. Learn more

  2. Aaron Morris 0 Reputation points
    2025-09-07T22:16:46.49+00:00

    This may be tangential to the overall problem, but the reason the MQTT User Properties are not being sent in the original code is because Python paho.mqtt defaults to MQTTv311 (and the code explicitly connects with this protocol above).

    In order to use UserProperties, you must connect with MQTTv5.

    client = mqtt.Client(client_id=device_id, protocol=mqtt.MQTTv5)
    

    Was this answer helpful?

    0 comments No comments

  3. Sedat SALMAN 14,385 Reputation points MVP
    2023-04-11T11:48:52.9+00:00

    Check the document for user properties [http://docs.oasis-open.org/mqtt/mqtt/v5.0/cs02/mqtt-v5.0-cs02.html#_Toc514345337] you can use the following

    import paho.mqtt.client as mqtt
    import paho.mqtt.properties as props
    from paho.mqtt.packettypes import PacketTypes
    
    final_payload = '{"datetime": "2023-03-30 06:20:30", "RaR": 123, "level": 750, "id": "1234"}'
    topic = "your_topic_here"
    
    # Your MQTT client connection code here
    
    publish_properties = props.Properties(PacketTypes.PUBLISH)
    publish_properties.UserProperty = ("Content-Type", "application/json")
    publish_properties.UserProperty = ("content-encoding", "UTF-8")
    
    client.publish(topic, payload=final_payload, qos=1, properties=publish_properties)
    
    

    Was this answer helpful?


Your answer

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