Search code examples
amazon-web-servicesaws-sdkaws-sdk-jsaws-event-bridge

AWS EventBridge putEvents does not accept Detail JSON array


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?


Solution

  • 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" }),