I am sending some html content inside json in the body of a REST callout from Salesforce to Dotdigital per their specs which is working well overall.

string.escapeHTML4 is handling most of the special characters I have tried in testing, but including a single backslash \ returns an error from the endpoint :

"message":"Could not parse the body of the request based on the content type \"application/json\" ERROR_BODY_DOES_NOT_MATCH_CONTENT_TYPE"

This could be an issue w the endpoint (dotdigital), but wondering if there is something else I should be using to escape the string?

Before I added the escapeHTML4 I was getting errors when the string included double quotes, but that is working fine now.

Note that double backslash (\\) does not return an error, just single.

  • Hi. WHat about triple ` \\\ `? Aug 16, 2019 at 17:41
  • triple backslash -> error; four backslash -> success; i see the pattern, but dont know what to make of it.
    – gorav
    Aug 16, 2019 at 22:14
  • 1
    if you're sending JSON content it's better to follow javascript/ecma script rules for escaping thus escapeEcmaScript() is a better choice.
    – Marcin Sz.
    Aug 16, 2019 at 22:56
  • escapeEcmaScript() is good - its handling all sorts of characters without any issue. however my next operation is no longer working properly. it is replacing carriage returns and line feeds w br html tags using replaceAll('\r\n|\r|\n', '<br />'); i've tried to find the right pattern but failed. will post that as a separate question unless you have any insight to share on this front. thx
    – gorav
    Aug 18, 2019 at 17:27
  • i cant quite figure out what is going on under the hood, but replaceAll('\\\\r\\\\n|\\\\r|\\\\n', '<br />') works w escapeEcmaScript
    – gorav
    Aug 18, 2019 at 20:20

1 Answer 1


I switched from hand rolling to creating the JSON by serializing an apex class using System.JSON.Serialize which handles the escaping.

With EscapeECMAScript I ran into issues with / characters like https://google.com coming over as https://google.com which I did not need.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .