如何在向节点 js 中的 azure 服务总线队列发送消息时将内容类型指定为 application/json?

问题描述

我正在使用 here 中提到的 @azure/service-bus 包和 sendMessages 函数向队列发送消息。 当我发送一个 javascript 数组 [{ name: "Albert Einstein","company": "xyz" }] 时,它给出了一个错误 TypeError: Provided value for 'message' must be of type ServiceBusMessage。因此,经过研究发现它添加body 键,例如[body:{name: "Albert Einstein","company": "xyz"}]。但这会插入内容类型为 application/xml 的记录。有什么办法可以指定 content-type:application/json 吗?

解决方法

您可以指定 contentType 如下:

const messages = [
    {
        body: { "name": "Albert Einstein","company": "xyz"},contentType: "application/json"
    }
]

请参考ServiceBusMessage

enter image description here