0

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){

            if(mpFieldMapping.containsKey(wfField.fieldName)){

                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

request.setBody(JSONObj.buildJSON(wr));

and here is the BuildJSON function

public static string buildJSON(JSONObj obj) {

    String s = System.JSON.serialize(obj);
    return s;
}
  • How are you populating the body content? Salesforce always represents string data in UTF-8. – David Reed Mar 7 at 16:34
  • @DavidReed - I've updated the code snippet to show how the JSON values are being set. – FredrikUNS Mar 7 at 17:03
  • How do you set the body on your outbound HTTP request? – David Reed Mar 7 at 17:08
  • @DavidReed - I've updated the question. thanks – FredrikUNS Mar 7 at 17:19
  • 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 at 0:20
0

@david reed.

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

Thanks for your time and my sanity!

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.