7

I am using AWS SDK v2.796.0

As per the documentation of putEvents, the Detail value needs to be a valid JSON string. https://docs.aws.amazon.com/eventbridge/latest/APIReference/API_PutEventsRequestEntry.html

However, it is not accepting a JSON array as string.

    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.'
    //     }
    //    ]
    //  }

Is this expected? Is there a way to use array in Detail?

Rashid Shaikh
  • 395
  • 4
  • 12
  • 1
    @Marcin see the answer below. The workaround is to wrap the array in another object. E.g `Detail: JSON.stringify({events: ["test1", "test2"])` – Rashid Shaikh Nov 30 '21 at 04:55

1 Answers1

6

This happens because you are using list in your entries2:

Detail: JSON.stringify([{ test: "test" }]),

If you just use object, it will work:

Detail: JSON.stringify({ test: "test" }),
Marcin
  • 215,873
  • 14
  • 235
  • 294