+ Start a Discussion
Kiran PandiyanKiran Pandiyan 

How can I solve this error in Apex REST Callout challenge ?

Global class AnimalLocator {
    Global static String getAnimalNameById(Integer numb){
        String response;
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/'+numb);
        request.setMethod('GET');
        HttpResponse resp = http.send(request);
        if(resp.getStatusCode()==200){
            Map<String,Object> results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());
            Map<String,Object> animal = (Map<String,Object>)results.get('animal');
            response = (String)animal.get('name');
        }
        return response;
    }
}
I'm getting the error "Method does not exist or incorrect signature : void getBody() from String type "
 
Best Answer chosen by Kiran Pandiyan
{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

Try below code for AnimalLocator class:-

public class AnimalLocator {
    
    public class AnimalInstanceClass {
        public Integer id;    
        public String name;    
        public String eats;    
        public String says;    
    } 
    
    public class JSONResponseClass{
        public AnimalInstanceClass animal;
    }
    
    public static String getAnimalNameById (Integer id) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        system.debug('response: ' + response.getBody());
        JSONResponseClass results = (JSONResponseClass) JSON.deserialize(response.getBody(), JSONResponseClass.class);
        system.debug('results= ' + results.animal.name);
        return(results.animal.name);
    }

}


Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com

All Answers

{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

Try below code for AnimalLocator class:-

public class AnimalLocator {
    
    public class AnimalInstanceClass {
        public Integer id;    
        public String name;    
        public String eats;    
        public String says;    
    } 
    
    public class JSONResponseClass{
        public AnimalInstanceClass animal;
    }
    
    public static String getAnimalNameById (Integer id) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://th-apex-http-callout.herokuapp.com/animals/' + id);
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        system.debug('response: ' + response.getBody());
        JSONResponseClass results = (JSONResponseClass) JSON.deserialize(response.getBody(), JSONResponseClass.class);
        system.debug('results= ' + results.animal.name);
        return(results.animal.name);
    }

}


Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
This was selected as the best answer
Kiran PandiyanKiran Pandiyan
Hi Pramod I got where the error is in my class, the line 10 should be 
Map<String, Object> results = (Map<String, Object>)JSON.deserializeUntyped(resp.getBody());

instead of 
Map<String,Object> results = (Map<String,Object>) JSON.deserializeUntyped(response.getBody());

SO the previous problem is solved

Now the Challenge is  Not yet complete... here's what's wrong: 
"The Apex test class 'AnimalLocatorTest' does not appear to be using the AnimalLocatorMock class correctly."

My Test Class 

@isTest
private class AnimalLocatorTest {
    static TestMethod void testGetLocCallout(){
        //Create the mock response based on the static resource
        StaticResourceCalloutMock mock1 = new StaticResourceCalloutMock();
        mock1.setStaticResource('GetAnimalLocResource');
        mock1.setStatusCode(200);
        mock1.setHeader('Content-Type', 'application/json;charset=UTF-8');
        // Associate the callout with a mock response
        Test.setMock(HttpCalloutMock.class, mock1);
        // Call method to test
        Integer i=1;
        String result = AnimalLocator.getAnimalNameById(i);
        //Verify mock response is not null
        System.assertNotEquals('chicken',result,
          'The animal name is returned wrongly');
        // Verify the array contains 3 items     
        //Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(result.getBody());
        //List<Object> animal = (List<Object>) results.get('animal');
        //System.assertEquals(null, animal,'There is no name for animal.');
    }

}


My Mock Class

@isTest
global class AnimalLocatorMock implements HttpCalloutMock {
	global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        response.setStatusCode(200);
        return response; 
    }
}
How can I solve it ?
{!pramod_nishane}{!pramod_nishane}
Hi Kiran,

Try below code:-

1) AnimalLocatorTest class**********-

@IsTest
global class AnimalLocatorTest {
    global HTTPresponse respond(HTTPrequest request) {
        Httpresponse response = new Httpresponse();
        response.setStatusCode(200);
        response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        return response;
    }
    @isTest
    public static void testAnimalLocator() {
        Test.setMock(HttpCalloutMock.class, new AnimalLocatorMock());
        //Httpresponse response = AnimalLocator.getAnimalNameById(1);
        String s =  AnimalLocator.getAnimalNameById(1);
        system.debug('string returned: ' + s);
    }

}

2) AnimalLocatorMock class**********-

@IsTest
global class AnimalLocatorMock implements HttpCalloutMock {
    
    global HTTPresponse respond(HTTPrequest request) {
        Httpresponse response = new Httpresponse();
        response.setStatusCode(200);
        response.setBody('{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}');
        return response;
    }

}

Let me know in case of any concerns.

Please mark this answer as the solution/ best answer if it solves your purpose so that it can help other community members.

Thanks,
Pramod Nishane
Salesforce Consultant
Varasi LLC
www.varasi.com
Kiran PandiyanKiran Pandiyan
Thanks Pramod ,
I just did something similar in Test.setMock line like you did and it worked for me. 
Thanks for you help.