Fixing webhooks erorrs
Troubleshooting webhooks can be a tricky task. Here we have some recommendations that will help you to find and fix errors in your webhook
Jinja string contains escaped single quote (\')
Assume that you have mapped a Custom variable like this
You can see that it is a valid Jinja statement that produces valid results.
Nevertheless, when you try to save the webhook it fails the save button turns green but in a moment it reverts to red and you can see 500 Internal Server Error in the Network tab of Browser's developer tools.
the error message says:
"error": ["expected token 'end of print statement', got Connor'"]
That happens because when you save the webhook it is converted to JSON and JSON standard does not allow escaping of single-quote character (so \' is not allowed in JSON strings) To overcome this, the Jinja template is converted to "{{ 'O\\'Connor' }}" and that brakes Jinja parsing later.
The fix is to use double quotes when defining stings. The following Jinja templates will work the same and will save without a problem
{{ "O'Connor" }}
{{ "O\'Connor" }}
Move webhook body to the bot (wh_body variable)
Sometimes webhooks can fail because of a broken JSON body. To inspect the body you can move it to the bot, and analyze it in the tester later.
1) Copy current body to a clipboard
2) Replace the body with {{ wh_body |safe }}
3) In the module where you call webhook, add SET VAR connection before
4) Name it wh_body, select type Jinja, and paste your body from step 1
Use echo endpoint
Another option to inspect the actual data passed by your webhook is to use free echo endpoints like Postman Echo API
1) Add Postman echo post webhook from the Webhook marketplace
2) Copy-paste the webhook body that you need to analyze
3) Replace the original webhook call with the Echo webhook
4) Analyze its response in tester
Important facts about webhooks
- If a webhook returns JSON object and does not have any Custom Variables mapped all keys of this object become Custom Varibales.
F.e. if a webhook returns {"name": "Alice", "age": 17} it will create two CVars: "name" and "age" - Response mapper runs only when the webhook call was successful (wh.status=200) If it is not successful none of CVars are assigned.