function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rahul Bhattad 4Rahul Bhattad 4 

How to HTML text in the body of Rest API callout

Hello,

I am trying to send HTML text in the request body for Rest API callout. I came to know that you cannot use Escape quotation marks used around HTML attributes like </br> as it breaks the JSON body. In Apex we cannout escape it by writing it like <\/ br> as it gives compilation error in class. How should I get around this situation so as to send HTML code without breaking JSON?
David Zhu 🔥David Zhu 🔥
If you have control on both sides, In Salesforce, you can encode the html text using EncodeUtil.base64Encode() method and put in request body.
On the other side, you just need to decode it.
AbhishekAbhishek (Salesforce Developers) 
Hi Rahul,

First, your HTML is wrong. It's <br />, not </br>. The </tag> means to close an open tag (<tag>), while <tag /> means "there is no content", and is semantically equal to <tag></tag>.

Second, you simply need to JSON.serialize(htmlString) to make your string JSON-safe.

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks.