如何在向节点 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

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...