[SalesForce] Pass json string in url

I have a url like

events.ktass.com/graze/v1/addSituationCustomInfo?auth_token='+cc+'&sitn_id='+bb+'&custom_info={incrf":'+aa+'}
I want to apend json string like this custom_info={'incrf':'aa'}

How can I achieve this?

Best Answer

This is JSON (note the double not single quotes):

{"incrf":"aa"}

To ensure that no characters that have special meaning in a URL are included, put the JSON String through EncodingUtil.urlEncode when you add it to the URL and through EncodingUtil.urlDecode when you take it out of the URL.

The above JSON encodes to:

%7B%22incrf%22%3A%22aa%22%7D

This approach is best used with quite short JSON (a few hundred characters) as maximum URL length limits across the internet are somewhat unpredictable.

Related Topic