In the challenge, it is specified :

The Apex class must be called 'AnimalLocator', have a 'getAnimalNameById' method that accepts an Integer and returns a String.

and i cannot understand the line :

public class AnimalLocator {
    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);
        HttpResponse response = http.send(request);
        if (response.getStatusCode() == 200) {
            Map<String, Object> results = (Map<String, Object>) JSON.deserializeUntyped(response.getBody());
            if(results.size() > 0){
                for (Object animal : results.values()) {
        return String.valueOf(response) ;

As the debug result, i get ->

DEBUG|{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}

The expected result must be ->

{"animal":{"id":1,"name":"chicken","eats":"chicken food","says":"cluck cluck"}}

i am unable to understand the issue!

  • I think that your id variable must be null. – Martin Lezer Aug 30 '17 at 7:47
  • See json.org for JSON syntax.The response you are getting is an array of animal names. A REST service might return such an array when you haven't asked for a specific value by supplying an id so check the id value you are requesting. – Keith C Aug 30 '17 at 7:48
  • That's not the case! id is not null – Saumya Gaikwad Aug 30 '17 at 7:48
  • when i browse to the url th-apex-http-callout.herokuapp.com/animals/2 the result is fine, but when i try to access from a callout, it isn't returning the expected result – Saumya Gaikwad Aug 30 '17 at 7:50
  • try with request.setMethod('GET'); – Sagar Thoriya Aug 30 '17 at 7:57

There is no problem in your method. It is working fine.

As @Martin & @Keith mentioned in the comments, you must check for Id is not null. and make sure you are passing Integer as param to this method while calling it.

    public static String getAnimalNameById( Integer id ){
        if( id != null ){
            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 'animal' key as a object
                Object animal = (Object) results.get('animal');
                System.debug('Received the following animal:');
                Map<String, Object> details = ( Map<String, Object> ) animal;
                String name = String.valueOf( details.get('name') );
                return name;
        return '';

and call it as:

System.debug( '----> '+AnimalLocator.getAnimalNameById(1) );

It will get you the animal name as "chicken".

USER_DEBUG|[113]|DEBUG|----> chicken

Thankyou @thatherahere The issue was here:

In the Mock i mentioned

global class AnimalLocatorMock implements HttpCalloutMock {
    // Implement this interface method
    global HTTPResponse respond(HTTPRequest request) {
        // Create a fake response
        HttpResponse response = new HttpResponse();
        response.setHeader('Content-Type', 'application/json');
        response.setBody('{"animals": ["majestic badger", "fluffy bunny", "scary bear", "chicken", "mighty moose"]}');
        return response; 

Instead, it should be

response.setBody('{"animal":{"id":2,"name":"bear","eats":"berries, campers, adam seligman","says":"yum yum"}}');

I have been placing the wrong text.


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.