AWS EventBridge putEvents 不接受 Detail JSON 数组
AWS EventBridge putEvents does not accept Detail JSON array
我正在使用 AWS SDK v2.796.0
根据 putEvents 的文档,详细信息值需要是有效的 JSON 字符串。
https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEventsRequestEntry.html
但是,它不接受 JSON 数组作为字符串。
const eventBridge = new AWS.EventBridge();
const entries = {
Entries: [
{
EventBusName: "busName",
Source: "api.user",
DetailType: "detailType",
Detail: JSON.stringify({ test: { test: ["test", "test2"] } }),
},
],
};
const rs = await eventBridge.putEvents(entries).promise();
console.log(rs);
// this passes
// {
// FailedEntryCount: 0,
// Entries: [ { EventId: 'a6176012-7310-2b84-a9b5-819956e2e3f9' } ]
// }
const entries2 = {
Entries: [
{
EventBusName: "busName",
Source: "api.user",
DetailType: "detailType",
Detail: JSON.stringify([{ test: "test" }]),
},
],
};
const rs2 = await eventBridge.putEvents(entries2).promise();
console.log(rs2);
// this fails
// {
// FailedEntryCount: 1,
// Entries: [
// {
// ErrorCode: 'MalformedDetail',
// ErrorMessage: 'Detail is malformed.'
// }
// ]
// }
这是预期的吗?有没有办法在Detail中使用数组?
发生这种情况是因为您在 entries2
:
中使用列表
Detail: JSON.stringify([{ test: "test" }]),
如果你只使用对象,它会起作用:
Detail: JSON.stringify({ test: "test" }),
我正在使用 AWS SDK v2.796.0
根据 putEvents 的文档,详细信息值需要是有效的 JSON 字符串。 https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEventsRequestEntry.html
但是,它不接受 JSON 数组作为字符串。
const eventBridge = new AWS.EventBridge();
const entries = {
Entries: [
{
EventBusName: "busName",
Source: "api.user",
DetailType: "detailType",
Detail: JSON.stringify({ test: { test: ["test", "test2"] } }),
},
],
};
const rs = await eventBridge.putEvents(entries).promise();
console.log(rs);
// this passes
// {
// FailedEntryCount: 0,
// Entries: [ { EventId: 'a6176012-7310-2b84-a9b5-819956e2e3f9' } ]
// }
const entries2 = {
Entries: [
{
EventBusName: "busName",
Source: "api.user",
DetailType: "detailType",
Detail: JSON.stringify([{ test: "test" }]),
},
],
};
const rs2 = await eventBridge.putEvents(entries2).promise();
console.log(rs2);
// this fails
// {
// FailedEntryCount: 1,
// Entries: [
// {
// ErrorCode: 'MalformedDetail',
// ErrorMessage: 'Detail is malformed.'
// }
// ]
// }
这是预期的吗?有没有办法在Detail中使用数组?
发生这种情况是因为您在 entries2
:
Detail: JSON.stringify([{ test: "test" }]),
如果你只使用对象,它会起作用:
Detail: JSON.stringify({ test: "test" }),