A quirk with slash-escapes (\n, \r, \s) in the Marketo webhook editor

Though the Marketo webhook feature is infinitely better than any other webhook/callout feature I’ve used — its flexibility is the reason FlowBoost can exist! — the webhook editor has a tiny learning curve.

If your webhook templates are adventurous enough[1] you may encounter this error:

The error message comes from Scala and (I’m mostly just quoting the screenshot!) means your payload can only use 8 known character sequences that start with a slash: \b \t \n \f \r \\ \" \'. Any other slash-escape-like thing throws an error.[2]

This is because Marketo pre-parses the template before sending it to the remote server. To be clear, this step uses the string parser within the Marketo platform. No network connection has been made at that point.

The result after pre-parsing is what’s sent over the network. The supported slash-escapes are turned into their real characters — \n turns into a real newline U+000A and so on — and then put on the wire.

Say you want to pass a JavaScript regular expression literal to your webhook-compatible service:

productInterests = {{Lead.Product Interest}}.split(/\s*;\s*/);

That’s gonna error out and it needs to be double-escaped (\\ is one of the 8 supported sequences, remember):

productInterests = {{Lead.Product Interest}}.split(/\\s*;\\s*/);

Similarly, if you’re sending static JSON strings with slash-escapes:

{
   "phone" : {{Lead.Phone Number}},
   "message" : "Today’s webinar starts in 10 minutes.\n\n—Sandy @ TEKNKL" 
}

You need to double-escape:

{
   "phone" : {{Lead.Phone Number}},
   "message" : "Today’s webinar starts in 10 minutes.\\n\\n—Sandy @ TEKNKL" 
}

Double-escapes in Admin » Webhooks become single-escapes at the remote end.

Quadruple-escaping is sometimes necessary

Predictably, things get hairier if you want the remote service to see a double-escaped sequence \\.

Let’s say I want the remote server to see a \\s (because within the remote service I need to escape). This won’t generate a hard error in Marketo, but it’s nevertheless broken:

The payload doesn’t have any disallowed slash-sequences, so no error. But you can see in the Activity Log that only single-slashed \s made it on the wire:

To send double-slash, use double-double-slash (quadruple-slash) in the Template:

Makes total sense given the rules, of course.

Notes

[1] Talking about the static parts of the template, not {{lead.tokens}} or other tokens.

[2] More forgiving languages just ignore the slash if they don’t recognize the sequence. For example, \z in a JS String has no special meaning and is the same as z; Scala considers the same thing an error. (Things like this remind you it’s really hard to be a grandmaster of multiple languages!)

Astute readers will note this also means JS/JSON \uNNNN escapes are not supported (since \u is not in the list). So if you need, say, 😮‍💨 just use those literal characters.

Remember, JS and JSON support UTF-8 and UTF-16 encoding, respectively. So it’s never mandatory to encode characters except for the ones with reserved meanings like \n and \". (People get confused and think non-ASCII/Latin-1 characters are inherently “special”, but it’s the special ASCII ones you should be worrying about!)