When I studied Apex REST Callouts, I faced with JSONException in Test Callouts section. How can I solve this problem?

I made prerequisites(AnimalsCallouts.cls, Static Resource) and made test class(AnimalsCalloutsTest cls) like below:


public class AnimalsCallouts {
    public static HttpResponse makeGetCallout() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        HttpResponse response = http.send(request);
        // If the request is successful, parse the JSON response.
        if (response.getStatusCode() == 200) {
            // Deserializes the JSON string into collections of primitive data types.
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            // Cast the values in the 'animals' key as a list
            List<Object> animals = (List<Object>) results.get('animals');
            System.debug('Received the following animals:');
            for (Object animal: animals) {
        return response;
    public static HttpResponse makePostCallout() {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setHeader('Content-Type', 'application/json;charset=UTF-8');
        request.setBody('{"name":"mighty moose"}');
        HttpResponse response = http.send(request);
        // Parse the JSON response
        if (response.getStatusCode() != 201) {
            System.debug('The status code returned was not expected: ' +
                response.getStatusCode() + ' ' + response.getStatus());
        } else {
        return response;

I uploaded txt file on Static Resource. txt file contains below:

{"animals": ["pesky porcupine", "hungry hippo", "squeaky squirrel"]}


private class AnimalsCalloutsTest {
    @isTest static  void testGetCallout() {
        // Create the mock response based on a static resource
        StaticResourceCalloutMock mock = new StaticResourceCalloutMock();
        mock.setHeader('Content-Type', 'application/json;charset=UTF-8');
        // Associate the callout with a mock response
        Test.setMock(HttpCalloutMock.class, mock);
        // Call method to test
        HttpResponse result = AnimalsCallouts.makeGetCallout();
        // Verify mock response is not null
            'The callout returned a null response.');
        // Verify status code
          'The status code is not 200.');
        // Verify content type   
          'The content type value is not expected.');  
        // Verify the array contains 3 items     
        Map<String, Object> results = (Map<String, Object>) 
        List<Object> animals = (List<Object>) results.get('animals');
        System.assertEquals(3, animals.size(),
          'The array should only contain 3 items.');          

Error Log:

Open Log [07L6F00005axeQqUAI]:testGetCallout System.JSONException: Unexpected character ('' (code 65279 / 0xfeff)): expected a valid value (number, String, array, object, 'true', 'false' or 'null') at input location [1,2]
Class.System.JSON.deserializeUntyped: line 11, column 1
Class.AnimalsCallouts.makeGetCallout: line 11, column 1
Class.AnimalsCalloutsTest.testGetCallout: line 12, column 1
  • What was the MIME type for your static resource? – Jayant Das Apr 19 at 2:01

0xfeff is the BOM (Byte Order Mark). This is a feature in UTF-16 that allows programs to detect which way the bytes were written (big-endian vs little-endian). However, Salesforce uses UTF-8, which does not require a BOM, and can indeed cause errors like this to occur. To fix this, you will want to replace the original file with a file saved in ASCII (ISO-8859-1/Latin) instead. Your code should work just fine after you fix the file's internal format.

  • Thanks sfdcfox. I replace the original file with a file saved in ANSI instead. It works ! – CJ Sohn Apr 24 at 6:00

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.