+ Start a Discussion
GerhardNewman2GerhardNewman2 

How to encode a string with UTF-8

Some of my customers have foreign characters in their company name.  For example "Hót Breád Bakéry"

I need to pass the company name to an external webservice as a string encoded in UTF-8.

 

Does anyone know how to encode the string?  Sounds simple, but I can't find the solution.

 

 

Best Answer chosen by Admin (Salesforce Developers) 
GerhardNewman2GerhardNewman2

Because I didn't know what I was doing my question is misleading.  What I wanted to do was pass data using a soap call, where the callout xml is encoded in utf8.  Running debug told me the headers were utf-8 so I thought it was an issue with some string text contained within the xml.   My thinking was incorrect the string was fine, but the callout xml was not being encoded with utf8.  Here is the solution in case anyone else gets stuck on this.

 

public class SQLOrderXML {
    public class CFWSINSoap {
        public String endpoint_x = 'http://a url';
        public Map<String,String> inputHttpHeaders_x;
        public Map<String,String> outputHttpHeaders_x;
        public String clientCertName_x;
        public String clientCert_x;
        public String clientCertPasswd_x;
        public Integer timeout_x;
        private String[] ns_map_type_info = new String[]{'http://a url/', 'SQLOrderXML'};
        
        public String SaveXML(String OrderXml) {
		    SQLOrderXML.SaveXML_element request_x = new SQLOrderXML.SaveXML_element();
            SQLOrderXML.SaveXMLResponse_element response_x;
		    request_x.inputHttpHeaders_x = new Map<String, String>();   
		    request_x.inputHttpHeaders_x.put('charset', 'utf-8');
            request_x.OrderXml = OrderXml;
            Map<String, SQLOrderXML.SaveXMLResponse_element> response_map_x = new Map<String, SQLOrderXML.SaveXMLResponse_element>();
            response_map_x.put('response_x', response_x);
            WebServiceCallout.invoke(
              this,
              request_x,
              response_map_x,
              new String[]{endpoint_x,
              'http://a url',
              'http://a url',
              'SaveXML',
              'http://a url',
              'SaveXMLResponse',
              'SQLOrderXML.SaveXMLResponse_element'}
            );
            response_x = response_map_x.get('response_x');
            return response_x.SaveXMLResult;
        }
    }
    public class SaveXMLResponse_element {
        public String SaveXMLResult;
        private String[] SaveXMLResult_type_info = new String[]{'SaveXMLResult','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://a url','false','false'};
        private String[] field_order_type_info = new String[]{'SaveXMLResult'};
    }
    public class SaveXML_element {
        public String OrderXml;
        private String[] OrderXml_type_info = new String[]{'OrderXml','http://www.w3.org/2001/XMLSchema','string','0','1','false'};
        private String[] apex_schema_type_info = new String[]{'http://a url','false','false'};
        private String[] field_order_type_info = new String[]{'OrderXml'};
        private Map<String, String> inputHttpHeaders_x = new Map<String, String>();
    }    
}

 This class was mostly auto generated using salesforce's wsdl to apex.

 

The added code to force utf8 is inside SaveXML (where the callout is made):

      request_x.inputHttpHeaders_x = new Map<String, String>();  
      request_x.inputHttpHeaders_x.put('charset', 'utf-8');

 

and this added to the end of the SaveXML_element (the request definition)

 

        private Map<String, String> inputHttpHeaders_x = new Map<String, String>();