cancel
Showing results for 
Search instead for 
Did you mean: 

Handling Webhooks with Multiple Payload Schemas (Reprise)

RussellJ
Deputy Chef III
Deputy Chef III

Hi

I was excited to find an EXACT match for my issue (as per the Subject Line) right here... it's just that the response didn't give me enough to get started 😥

I have a SaaS system to which I can subscribe to webhooks.  But there will be a variety of different JSON payloads being sent to my single Recipe, depending on the Event being sent.

Therefore I need to branch my workflow depending on the type of Event/JSON  being received.

When I follow the guided setup for a new Recipe of type "Trigger from a Webhook" it asks for a sample event and then configures the schema of the Trigger accordingly.  But obviously that won't capture the variety of Events being received.

Could someone give me the "dummies guide" to how to achieve what I'm after in Workato?

If JSON Event = "Create" then... {expect this JSON payload and map these values to datapills} and then go do stuff

If JSON Event = "Update" then... {expect this JSON payload and map these values to datapills} and then go do stuff

etc

The bit that's not clicking with me is simply how to replace the single guided JSON mapping for the Trigger with alernative nuanced logic (as above).  There  can be only one Trigger for a Recipe, so how to handle different incoming JSON payloads? I'm sure the "Parse JSON Document" action is involved but I ain't seeing how it comes together! 

It feels like this must be an FAQ, so I think a simple "How To" that others could use would be generically helpful.

Thanks in advance

Russell

1 ACCEPTED SOLUTION

My friend, you may be overthinking this...

From my first response:

I would just ignore the schema entirely and pass the "Payload" object on the webhook trigger into a JSON parser.

The webhook's Payload data pill will be a hash of anything you send to the webhook. It doesn't need to match the input schema.

Here's my webhook trigger, completely undefined other than POST JSON:

webhook1.jpg

Here's my cURL to call it from Terminal:

curl -X POST "https://webhooks.workato.com/webhooks/rest/top_secret/test" \
-H "Content-Type: application/json" \
-d '{"event_type": "do stuff", "anything": "yadayadayada"}'

Here's my JSON parser to capture the event type:

webhook2.jpg

And that's a wrap!

From here you'll have your event type parsed, and then you can route to other JSON parsers. When calling other functions, do the exact same thing as the JSON parser: pass the Payload as .to_json and parse it in the next recipe.

 

View solution in original post

8 REPLIES 8

RussellJ
Deputy Chef III
Deputy Chef III

Hi Gary

I understand every single word in your replies (and really appreciate your time and assistance!)... it's just that I still can't see how to proceed 😞

I've got a Recipe that receives the webhook call.  The idea is that this will just then effectively act as a switch to invoke the relevant Recipe Function, which will be passed the payload and can then process that using a JSON Parser.

I'm 100% with you... in theory.

But I need to define a JSON schema for the trigger in the initial recipe.  But the precise format of the JSON payload will vary depending on the event type.  So it needs to be generic to handle any event format.

So my questions are:

1) How do I define a generic JSON payload in the Recipe trigger?

2) How do I pass the generic JSON payload to a Recipe Function?

I can define a specific JSON payload in the trigger fine (and the guided setup makes this a snap).  And I could then pass this speficic payload to a Recipe Function (by defining each discrete item to be passed).

The bit that I'm missing is how to simply treat any incoming JSON as "payload" and then pass (said generic payload) onto a Recipe Function.

My friend, you may be overthinking this...

From my first response:

I would just ignore the schema entirely and pass the "Payload" object on the webhook trigger into a JSON parser.

The webhook's Payload data pill will be a hash of anything you send to the webhook. It doesn't need to match the input schema.

Here's my webhook trigger, completely undefined other than POST JSON:

webhook1.jpg

Here's my cURL to call it from Terminal:

curl -X POST "https://webhooks.workato.com/webhooks/rest/top_secret/test" \
-H "Content-Type: application/json" \
-d '{"event_type": "do stuff", "anything": "yadayadayada"}'

Here's my JSON parser to capture the event type:

webhook2.jpg

And that's a wrap!

From here you'll have your event type parsed, and then you can route to other JSON parsers. When calling other functions, do the exact same thing as the JSON parser: pass the Payload as .to_json and parse it in the next recipe.

 

RussellJ
Deputy Chef III
Deputy Chef III

OK, so the bit I was missing was how to get a handle on the basic Payload in the trigger.

The key piece of missing knowledge was simply to switch the Document field of the JSON Parser into Formula Mode and then use .to_json on the Payload value from the Trigger.

That right there will be the GOLD that future generations will thank me for, for so shamelessly parading my ignorance 🤣.  But most importantly, they will thank you Gary for taking the time to patiently guide me through this!!

To my credit I'd figured out the need to switch to Formula mode but was getting stuck with a conversion error, so I was nearly there 😀

Many thanks again!

No problem, glad we got to the answer!

Workato encodes recipe data as Ruby hashes (ex: {"key" => "value")), so using .to_json is needed to convert it in order for the JSON parser to work. If you have a "string JSON" (not stored as hash) this will work in the parser without any conversion.