JSON Webhooks

Hey,

I try to build a simple Hipchat notification by using webhooks. If I use simple static messages my notifications arrive. How do I create a JSON payload body with mustache? I tried the follwing configuration:

"webhook" : {
    "method" : "POST", 
    "scheme": "https",
    "host" : "api.hipchat.com",
    "port": 443,
    "path": "/v2/room/watchers/notification", 
    "headers" : {
      "Authorization" : "Bearer ROOM_TOKEN",
      "Content-Type": "application/json"
    },
    "body" : "{ \"message\": \"{{ctx.payload.hits.total_hits}}\" }"
}

But the notifications does not arrive my room. If I use a static "body" : "{ \"message\": \"xxx\" }" I receive my messages.

So how to build JSON payload for my body? It seems the template would now work with my JSON curly braces.

Oh I figured out my problem... The message is empty because there is no field total_hits. Your documentation contains many errors. But there is no Edit Button as in your open source products.

If I use "body" : "{ \"message\": \"{{ctx.payload.hits.total}}\" }" it's works fine except for the ugly escaped JSON string syntax.

Hi Flenco,
You can also specify the body as a json template like so :

"body" : {
  "inline" : { "message" : "{{ctx.payload.hits.total}}" }
}

Hi flecno, thanks for reporting this documentation bug. We are fixing it now.

I have the same issue with not being able to deserialize the data in the web server code. I tried @GaelTadh and I did notice a difference with my body code and I can see how that code would work with a single param like the total hits.

My issue however is that I am looking at sending the records that came back in the search as I need to extract some information out of each document and perform some tasks on each one. Is there a way to JSONify the search results to accomplish this? Or is there a better way to do this via a transform or script?

Thanks, @GaelTadh!!!

@GaelTadh's solution is also work to slack 500 error.
This is my fault webook with slack:

....
"body": "payload = { 'text': 'error test\n 400 errors count: {{ctx.payload.hits.total}} \n' blah blah}"
 ...

Change to:

# In header, content type is 'application/json'
 ....
"body": { "inline": { "text" : "error test\n 400 errors count: {{ctx.payload.hits.total}} \n blah blah"} }
...

Hope this is help to someone.

2 Likes

Hi

is there a way to send the complete payload received through webhook as JSON
i want to send the complete payload i received based on conditions

I have the exact same question as Pransh. Can someone please answer it? The webhook data is not returning as JSON and I want to know why and what can be done about it. Otherwise, I will need to create a script to do the conversion, but that should be an unnecessary step in the pipeline.

Hey,

please open new subjects instead of reviving old threads - a lot has changed since almost one and a half years :slight_smile:

First, if the data returned is not JSON, and you want to it to be JSON you need to convert it.
Since Elasticsearch 2.4 there is an additional mustache helper handler called toJson, which may help you.

--Alex

Hi iantayrob

"condition" : {
"compare" : {
"ctx.payload.hits.total" : {
"gt" : 0
}
}
},
"transform" : {
"script" : "return [ body: groovy.json.JsonOutput.toJson(ctx.payload)]"
},

Use this and you will receive the payload in JSON