我使用以下代码对执行PubSub请求:
pushMessages = async function()
{
//const collection = context.services.get(`mongodb-atlas`).db(`billing`).collection(`projectdata`);
var needle = require('needle');
var postData = {
"message": "hello world"
}
needle
.post('https://pubsub.googleapis.com/v1/projects/topicname:publish', postData, { json: true },
function(err, resp) {
if (err)
console.log('Error: ' + JSON.stringify(err.message));
else
console.log('OK.'+ JSON.stringify(resp.body));
});
}我不断地得到:
{"error":{"code":400,"message":"Invalid JSON payload received. Unknown name \"message\": Cannot find field.","status":"INVALID_ARGUMENT","details":[{"@type":"type.googleapis.com/google.rpc.BadRequest","fieldViolations":[{"description":"Invalid JSON payload received. Unknown name \"message\": Cannot find field."}]}]}}. 对如何解决这个问题有什么建议吗?
发布于 2022-02-09 12:03:45
您需要一个base64-encoded string来发布到google /sub中,就像文档页面上显示的那样。因此,不只是将数据转换为json才能工作,您还需要对其进行编码。
请参考node.js客户端库中的云酒吧/潜艇。
const data = JSON.stringify({foo: 'bar'});
const dataBuffer = Buffer.from(data);
const messageId = await pubSubClient.topic(topicName).publish(dataBuffer);
...您还可以查看这个rest/v1/projects.topics/publish上的API 链接详细信息。
https://stackoverflow.com/questions/71046956
复制相似问题