# How to send aggregation key one by one to webhook action

**URL:** <https://discuss.elastic.co/t/how-to-send-aggregation-key-one-by-one-to-webhook-action/328102>\
**Category:** Kibana\
**Tags:** painless\
**Created:** [March 20, 2023, 5:20pm UTC](https://discuss.elastic.co/t/how-to-send-aggregation-key-one-by-one-to-webhook-action/328102 "2023-03-20T17:20:20Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sai7276p](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sai7276p/32/117764_2.png) [@sai7276p](https://discuss.elastic.co/u/sai7276p)\
**Post date:** [March 20, 2023, 5:20pm UTC](https://discuss.elastic.co/t/how-to-send-aggregation-key-one-by-one-to-webhook-action/328102/1 "2023-03-20T17:20:20Z")

</div>

Hi

I am trying to send an aggregated hosts keys to webhook actions in parameters fields..  
When I use single action and this template  
//{{#ctx.payload.hosts}}{{key}} {{/ctx.payload.hosts}}  
It perfectly sends an array in the parameter, like below in Hostname,

Watcher Result

```auto
"actions": [
      {
        "id": "my_webhook",
        "type": "webhook",
        "status": "simulated",
        "webhook": {
          "request": {
            "host": "###############",
            "port": ######,
            "scheme": "https",
            "method": "post",
            "path": "/jenkins/job/Testv2/buildWithParameters",
            "params": {
              "Hostname": "CZ98NWOMAN CZ98PORIAN CZ98DSORAN CZ98DORAN "
            },

```

But when I use iteration in webhook action, it return null in the parameter for all iterations,  
If you see below, watcher returning null in the Hostname parameter ""

```auto
"actions": [
      {
        "id": "my_webhook",
        "type": "webhook",
        "status": "simulated",
        "number_of_actions_executed": 6,
        "foreach": [
          {
            "webhook": {
              "request": {
                "host": "###############",
                "port": ###,
                "scheme": "https",
                "method": "post",
                "path": "/jenkins/job/Testv2/buildWithParameters",
                "params": {
                  "Hostname": ""
                },

```

Please help.. How can I parse an array to send a key at a time to each webhook iteration...

Watcher :

```auto
{
  "trigger": {
    "schedule": {
      "interval": "5m"
    }
  },
  "input": {
    "search": {
      "request": {
        "search_type": "query_then_fetch",
        "indices": [
          "metricbeat-*"
        ],
        "rest_total_hits_as_int": true,
        "body": {
          "aggs": {
            "host": {
              "terms": {
                "field": "host.hostname",
                "order": {
                  "disk_usage": "desc"
                }
              },
              "aggs": {
                "disk_usage": {
                  "max": {
                    "field": "system.memory.used.pct"
                  }
                }
              }
            }
          },
          "query": {
            "bool": {
              "filter": [
                {
                  "range": {
                    "@timestamp": {
                      "gte": "now-{{ctx.metadata.window_period}}"
                    }
                  }
                },
                {
                  "range": {
                    "system.memory.used.pct": {
                      "gte": "{{ctx.metadata.threshold}}"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "condition": {
    "compare": {
      "ctx.payload.hits.total": {
        "gt": 0
      }
    }
  },
  "actions": {
    "my_webhook": {
    "foreach" : "ctx.payload.hosts", 
      "max_iterations" : 15,
      "webhook": {
        "scheme": "https",
        "host": "##############",
        "port": #####,
        "method": "post",
        "path": "/jenkins/job/Testv2/buildWithParameters",
        "params": {
          "Hostname": "{{#ctx.payload.hosts}}{{key}} {{/ctx.payload.hosts}}"
        },
      "auth": {
          "basic": {
            "username": "########",
            "password": "############"
          }
        }
      }
    }
  },
  "metadata": {
    "window_period": "5m",
    "threshold": 0.001
  },
  "transform": {
    "script": {
      "source": "def threshold_p = ctx.metadata.threshold*100; return ['threshold': (int)threshold_p, 'hosts': ctx.payload.aggregations.host.buckets.stream().map(p -> [ 'key': p.key, 'disk_usage': (int) (p.disk_usage.value*100)]).collect(Collectors.toList()) ];",
      "lang": "painless"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 17, 2023, 5:20pm UTC](https://discuss.elastic.co/t/how-to-send-aggregation-key-one-by-one-to-webhook-action/328102/2 "2023-04-17T17:20:34Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
