I'm trying to send a JSON to an external API in UTF-8 but the vendor tells me it's coming through as garbage.

I've set the following headers:

for(JSONObj.workflowRequestTableFields wfField: wr.Requestform.WorkflowRequestInfo.workflowRequestTableFields){


                wfField.fieldValue = string.valueOf(sObj.get(mpFieldMapping.get(wfField.fieldName)));  //populating the JSON here          


request.setHeader('Charset', 'UTF-8');
request.setHeader('Content-Type', 'application/json;charset=UTF-8');
request.setHeader('Content-Encoding', 'utf-8');

But it's still not being received correctly. Is there another header I should be setting ?

The body is being set as follows


and here is the BuildJSON function

public static string buildJSON(JSONObj obj) {

    String s = System.JSON.serialize(obj);
    return s;
  • 1
    How are you populating the body content? Salesforce always represents string data in UTF-8. – David Reed Mar 7 '19 at 16:34
  • @DavidReed - I've updated the code snippet to show how the JSON values are being set. – FredrikUNS Mar 7 '19 at 17:03
  • How do you set the body on your outbound HTTP request? – David Reed Mar 7 '19 at 17:08
  • @DavidReed - I've updated the question. thanks – FredrikUNS Mar 7 '19 at 17:19
  • 1
    I'm a little mystified by this since JSON.serialize() should produce valid UTF-8 - Salesforce strings are always UTF-8. Can you get some more debugging detail for us? An example of a "garbage" message as shown in Salesforce and as received by the vendor? – David Reed Mar 8 '19 at 0:20

@david reed.

Having spoken to the vendor again. It turns out they were incorrectly decoding the JSON.

Thanks for your time and my sanity!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.