尝试发送带有代理属性的 Azure 服务总线消息时出现 400 错误

Getting 400 error when trying to send an azure service bus message with broker properties

我有一个小型 python 应用程序可以将信息发送到我的 Azure 服务总线。我注意到每条消息都有 "broker_properties" 字典,并且有一个名为 "Label" 的 属性,稍后我可以从服务总线访问它。

我正在尝试发送填充 属性:

的消息
properties = {"Label":label}
msg = Message(bytes(messagebody, "utf-8"), bus_service, broker_properties=properties)
bus_service.send_queue_message("queue", msg)

但这似乎不起作用。当执行上面的命令时,我从 Azure 返回一个错误:

The value '{'Label': 'testtest'}' of the HTTP header 'BrokerProperties' is invalid.

这是 Python Azure SDK 中的错误还是我做错了什么?

根据您的代码,问题是由使用 Python dict 对象作为 broker_properties 的值引起的,但 broker_properties 值应该是 json 细绳。请参考 Azure SDK 中的测试 code for Python on GitHub.

所以请修改您的代码如下。

properties = '{"Label": "%s"}' % label

import json
properties = json.dumps({"Label":label})